Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

question: 1 why do we write functions? - to make our code easier to und…

Question

question: 1
why do we write functions?

  • to make our code easier to understand by giving a readable name to a group of instructions
  • to avoid writing repeated code
  • to make our code reusable
  • all of the above

question: 2
consider the following function:

function rolldice() {
let roll1 = randomizer.nextint(1, 6);
let roll2 = randomizer.nextint(1, 6);
console.log(\you rolled \ + roll1 + \ and \ + roll2);
}

what would be the correct way to call this function?

  • rolldice;
  • rolldice();
  • function rolldice;
  • function rolldice();

Explanation:

Response
Question 1
Brief Explanations
  • Naming a group of instructions with a function makes code understandable.
  • Functions help avoid repeating code (DRY principle).
  • Functions can be called multiple times, making code reusable. So all options are correct.
Brief Explanations
  • To call a function in JavaScript, we use the function name followed by parentheses ().
  • rollDice; just references the function, not calls it.
  • function rollDice; and function rollDice(); are incorrect syntax for calling (they are for declaration or incorrect usage).
  • rollDice(); is the correct way to invoke the function.

Answer:

D. All of the above

Question 2