Which of the following code snippets properly calculates the cost of 5 watermelo
ID: 3886612 • Letter: W
Question
Which of the following code snippets properly calculates the cost of 5 watermelons, which cost $8 each?
Snippet A:
function calculate(cost, 5, *) {
return cost * 5;
}
calculate(cost, 5);
Snippet B:
function calculate(cost, 5) {
return cost * 5;
}
calculate(5 * 8);
Snippet C:
function calculate(cost) {
return cost + 5;
}
calculate(cost);
Snippet D:
function calculate(cost) {
return cost * 5;
}
calculate(8);
Select your answer:
A. Snippet C
B. Snippets A & D
C. Snippets A, B, & D
D. Snippets D
E. Snippets B & D
Explanation / Answer
Snipped D will return the correct cost
Explanation:
Snippet A:
function calculate(cost, 5, *) {
return cost * 5;
}
calculate(cost, 5);
Here we are not passing the cost. Also, the function declaration is not correct.
function calculate(cost, 5) {
return cost * 5;
}
calculate(5 * 8);
In this snippet, the value of cost is passed as (5*8 i.e. 40) instead of 8. So it will return incoorect cost i.e. 200. This would have given the correct result if we called the function as calculate(8).
Snippet C:
function calculate(cost) {
return cost + 5;
}
calculate(cost);
Here add operation is being done instead of multiplication. Hence wrong result.
Snippet D:
function calculate(cost) {
return cost * 5;
}
calculate(8);
This will give the correct result as the value of the variable 'cost' will be 8 and the return value will be the cost of 5 watermelons i.e. 40.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.