Use the image attached to declare and call functions in Applab.
1 point
1
Question 1
1.
Why do we write functions?
I. Make our code easier to understand by giving a readable name to a group of instructions
II. Avoid writing repeated code
III. Make our code reusable
1 point
1
Question 2
2.
When we put a variable between parenthesis, what is it called?
1 point
1
Question 3
3.
In the following function printThreeTimes:
function printThreeTimes(word){
println(word);
println(word);
println(word);
}
What is the parameter of the function?
1 point
1
Question 4
4.
On which line is the "call" to the function doubleNumber on the video? (video: 4:50 )
1 point
1
Question 5
5.
What would I get if I called doubleNumber(10); ? (video: 5:01 )
1 point
1
Question 6
6.
After you watched all the video, Define the printNumbers function thattakes three integers and print them in given order
functionprintNumbers(two, one, zero){
println(two);
println(one);
println(zero);
}
Call printNumbers function whenever printNumberBtn is clicked (hint:onEvent("printNumberBtn", "click", function(){ call functions here } );
varzero =0;
varone=1;
vartwo= 2;
printNumbers(zero, one, two);
What will be displayed on the screen?
1 point
1
Question 7
7.
Revise the printNumbers function to print given parameters in below order.
functionprintNumbers(two, one, zero){
println(two);
println(zero);
println(zero);
println(one);
}
Revise the code inside printNumberBtn click event function as below.
printNumbers(12, 17, 65);
What will be displayed on the screen?
1 point
1
Question 8
8.
“It’s a bird! It’s a plane! No, it’s Superman!”
If it’s not a bird and it’s not a plane, it must be Superman.
We want to write a function isSuperman that takes in two boolean parameters isBird and isPlane and returns true if it is in fact Superman, not a bird and not a plane, and false otherwise.
Which of the following functions is the correct implementation of isSuperman?
1 point
1
Question 9
9.
True or False: Functions must have parameters.
Function with Parameters 2
Watch the video above and please use your remixed project.
Image attached shows how to call doubleNumber(x) inside doubleBtn click event function. Implement sumBtn event function with sum(num1, num2) calls inside your code.
1 point
1
Question 10
10.
How many parameters does the function sum use?
1 point
1
Question 11
11.
What would get printed if we added the code,
sum(4, 3); ?
Function with Return Values
1 point
1
Question 12
12.
Revise your doubleNumber() function to return value instead of printing it.
Replace all code inside doubleNumberBtn to below segment.
var numApples=4;
var twiceAsMany=doubleNumber(numApples);
println("Before : "+numApples);
println("After : "+twiceAsMany);
Will will be printed when this code is run?
1 point
1
Question 13
13.
What happens if you try to print a function call when the function doesn't have a return value?
console.log( printNumbers(13,15,17));
console.log ( sum(13,15,17) );
1 point
1
Question 14
14.
Code along with the video3 and revise the function doubleNumber to return a value.( video:2:15)
What happens if you try to call the function with a return value like below?
doubleNumber(6);
randomNumber(1,10);
1 point
1
Question 15
15.
Revise doubleNumber(x) function and call it inside doubleNumberBtn as shown below.