Wheemangga

[JavaScript] Todo List only using Prompt

Apr 24th, 2022 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A S̶i̶m̶p̶l̶e̶  Complicated todo list using JavaScript.
  2.  
  3. // ENGLISH
  4.  
  5. // Variables:
  6. // todo - A really constant var that'll only works when parsed into an Object Key & Values
  7. // ^ Don't bother this var, I made it only to enable todoKey & todoValues to !null
  8. // todoKey - Object Keys of todo
  9. // todoValues - You know it already
  10. // quit - It's uhh.. something that makes the loop infinite until we input a quit opt
  11.  
  12. // How does it work
  13. // You first need to link this script to a simple html doc, then just run it.
  14. // The way this todo works is that they prompt you to input 1 out of 4 opts.
  15. // If you enter something the code don't understand it returns you back to the main while loop
  16. // Such as 'new' and 'delete'.
  17. // The delete.. deletes a todo from the list.
  18. // Note that I made the index from 1 so it kinda look like a real todo list.
  19. // Don't worry, you can delete the todo using the exact number as the index. I coded it below (line 63-68)
  20. // I don't find any bug in this code, I apologize if there's any, sadly I'm just starting to learn JavaScript for 2 weeks by the time I code this.
  21.  
  22. const todo = {
  23.   1: "Masak aer",
  24. };
  25. const todoKey = Object.keys(todo);
  26. const todoValues = Object.values(todo);
  27. let quit = false;
  28.  
  29. todoKey.shift();
  30. todoValues.shift();
  31.  
  32. let input;
  33. while (quit === false) {
  34.   input = prompt(
  35.     "[New] - Add Todo\n[List] - List todo\n[Delete] - Delete todo\n[Quit] - Quit todo\n\nMasukkan opsi"
  36.   );
  37.   while (!input) {
  38.     input = prompt("Masukkan opsi!");
  39.   }
  40.   if (input.toLowerCase() === "new") {
  41.     input = prompt("Masukkan todo baru");
  42.     while (!input) {
  43.       input = prompt("Masukkan todo baru!");
  44.     }
  45.  
  46.     todoKey.push(todoKey.length + 1);
  47.     todoValues.push(input);
  48.   }
  49.  
  50.   if (input.toLowerCase() === "list") {
  51.     console.log("\n");
  52.     for (let i = 0; i < todoKey.length; i++) {
  53.       console.log(`${[i + 1]}: ${todoValues[i]}`);
  54.     }
  55.   }
  56.  
  57.   if (input.toLowerCase() === "delete") {
  58.     input = parseInt(prompt(`Yang mana yang ingin kamu delete?`));
  59.     if (!todoKey[input] === false && !todoValues[input] === false) {
  60.       if (input === 1) {
  61.         todoKey.splice(input, 1);
  62.         todoValues.splice(input, 1);
  63.       } else {
  64.         todoKey.splice(input - 1, 1);
  65.         todoValues.splice(input - 1, 1);
  66.         console.log("UDAH");
  67.       }
  68.     } else {
  69.       console.log("Tidak ada index itu!");
  70.     }
  71.   }
  72.  
  73.   if (input === "q" || input === "Q" || input === "quit") {
  74.     alert("Ok");
  75.     break;
  76.   }
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment