1
1

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

udemy課題自力作成し理解度UP

.js
let input = prompt('こちらに以下を入力せよ(new, list, delete, quit)')
const todos = ['散歩する', '走る', '筋トレする', 'ご飯を食べる'];
// || ではないのがポイント
  while (input !== 'quit' && input !== 'q') {
    if (input === 'list') {
      for (let i=0; i < todos.length; i++) {
        console.log(`${i}:${todos[i]}`);
      }
    } else if (input === 'new') {
      let newTodo = prompt('新しいtodoを入力せよ');
      todos.push(newTodo);
      console.log(`『${newTodo}』を追加しました`)
    } else if (input === 'delete') {
    // parseInt使用しないと数字以外でも削除できてしまう
      let index = parseInt(prompt('削除するイデックスを入力せよ'));
      if (!Number.isNaN(index)) {
        let deleted = todos.splice(index, 1);
        console.log(`『${deleted}』を削除しました`);
      } else {
        console.log('有効なインデックスを入力せよ');
      }
    }
    input = prompt('こちらに以下を入力せよ(new, list, delete, quit)')
  }
  alert('アプリを終了しました');
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?