Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A S̶i̶m̶p̶l̶e̶ Complicated todo list using JavaScript.
- // ENGLISH
- // Variables:
- // todo - A really constant var that'll only works when parsed into an Object Key & Values
- // ^ Don't bother this var, I made it only to enable todoKey & todoValues to !null
- // todoKey - Object Keys of todo
- // todoValues - You know it already
- // quit - It's uhh.. something that makes the loop infinite until we input a quit opt
- // How does it work
- // You first need to link this script to a simple html doc, then just run it.
- // The way this todo works is that they prompt you to input 1 out of 4 opts.
- // If you enter something the code don't understand it returns you back to the main while loop
- // Such as 'new' and 'delete'.
- // The delete.. deletes a todo from the list.
- // Note that I made the index from 1 so it kinda look like a real todo list.
- // Don't worry, you can delete the todo using the exact number as the index. I coded it below (line 63-68)
- // 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.
- const todo = {
- 1: "Masak aer",
- };
- const todoKey = Object.keys(todo);
- const todoValues = Object.values(todo);
- let quit = false;
- todoKey.shift();
- todoValues.shift();
- let input;
- while (quit === false) {
- input = prompt(
- "[New] - Add Todo\n[List] - List todo\n[Delete] - Delete todo\n[Quit] - Quit todo\n\nMasukkan opsi"
- );
- while (!input) {
- input = prompt("Masukkan opsi!");
- }
- if (input.toLowerCase() === "new") {
- input = prompt("Masukkan todo baru");
- while (!input) {
- input = prompt("Masukkan todo baru!");
- }
- todoKey.push(todoKey.length + 1);
- todoValues.push(input);
- }
- if (input.toLowerCase() === "list") {
- console.log("\n");
- for (let i = 0; i < todoKey.length; i++) {
- console.log(`${[i + 1]}: ${todoValues[i]}`);
- }
- }
- if (input.toLowerCase() === "delete") {
- input = parseInt(prompt(`Yang mana yang ingin kamu delete?`));
- if (!todoKey[input] === false && !todoValues[input] === false) {
- if (input === 1) {
- todoKey.splice(input, 1);
- todoValues.splice(input, 1);
- } else {
- todoKey.splice(input - 1, 1);
- todoValues.splice(input - 1, 1);
- console.log("UDAH");
- }
- } else {
- console.log("Tidak ada index itu!");
- }
- }
- if (input === "q" || input === "Q" || input === "quit") {
- alert("Ok");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment