I have a big question (please help)

so , this is not my code (i don’t know whom it is ) but =

function decision() {
  return Math.random() > 0.5;
}

const brainstormDinner = () => {
  return new Promise((resolve, reject) => {
    if (decision()) {
      const options = ["salad", "ramen", "eggs", "chicken"];
      let index = 0;
      const makeDecision = () => {
        if (index < options.length) {
          setTimeout(() => {
            console.log(`Should I make ${options[index]} for dinner?`);
            index++;
            makeDecision();
          }, 4000); 
        } else {
          setTimeout(() => {
            const randomIndex = Math.floor(Math.random() * options.length);
            const selectedOption = options[randomIndex];
            console.log(`I have decided to make ${selectedOption} for dinner!`);
            setTimeout(() => {
              console.log(`I'm going to make ${selectedOption} for dinner.`);
              resolve(selectedOption);
            }, 4000); 
          }, 2999); 
        }
      };
      console.log(`Should I make ${options[index]} for dinner?`);
      index++;
      makeDecision();
    } else {
      setTimeout(() => {
        console.log("Hmmm... I can't decide.");
        setTimeout(() => {
          console.log("I think I'm gonna starve to death.");
          reject("I think I'm gonna starve to death");
        }, 4000); 
      }, 0); 
    }
  });
};

module.exports = brainstormDinner;

and the code works , but i’m wondering why and how it works ,
at line 32 we have =

    index++;
      makeDecision();
    } else {  // here
      setTimeout(() => {
        console.log("Hmmm... I can't decide.");

how does that else even exist , it doesnt have any if to be connected to , and even the } behind it is not opened anywhere else .

can someone please explain

probably meant to be tied to

if (decision()) 

but be warned it’s always considerably harder to understand another person’s code. I would just focus on the high-level big picture idea and not the syntax. If you want to reproduce it write a copy of the idea yourself from the ground up.

3 Likes

As toastedpitabread noted, it is paired with the if statement on line 7

If you have a JS code editor (you can search online for “js online” or similar terms and it will show plenty of results for free online editors/compilers), it will highlight the matching curly braces or parentheses.

For Example, look at the screenshots below. I clicked next to the closing curly brace on line 32 and it automatically highlighted the opening curly brace on line 7.

scrn1
scrn2

1 Like

exactly , but its been a while that im doing this , i just want to see other minds and things that people probably do

i have , put i never scrolled up enough to see it :sweat_smile: