1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

javascript

Posted at

学習備忘録
自力で作ると理解度UP

aa.js
let maximum = parseInt(prompt('始めに0以外の数字を入力せよ'));
  while (isNaN(maximum) || maximum <= 0) {
    maximum = parseInt(prompt('有効な数字を入力せよ'));
  }

  const targetNum = Math.floor(Math.random() * maximum) +1;

  let answerNum = prompt('答えの数字を決めました。予想して0以外の数字を入力せよ');
  let count = 1;

    while (parseInt(answerNum) !== targetNum) {
      if (answerNum === 'q') break;

      if (isNaN(answerNum) || answerNum <= 0) {
        answerNum = prompt('0以外の有効な数字を入力せよ');
        continue;
      }
      count ++;
      if (answerNum > targetNum) {
        answerNum = prompt('数字が大きすぎます。再度入力せよ。');
      } else {
        answerNum = prompt('数字が小さすぎます。再度入力せよ。');
      }
    }
    if (answerNum === 'q') {
      alert('リタイアしました。また挑戦してください。');
    } else {
      alert(`正解!${count}回で当たりました。`);
    }
    
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?