QUESTION IMAGE
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();
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;andfunction rollDice();are incorrect syntax for calling (they are for declaration or incorrect usage).rollDice();is the correct way to invoke the function.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
D. All of the above