AP CSP Javascript

Last updated almost 3 years ago
16 questions
Note from the author:
Functions with Parameters and Return Values
Functions with Parameters 1
Watch the video above https://www.youtube.com/watch?v=31HHVsWU1Fc&t=283s until 4:00 then remix the code below to practice afterwards.
https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I

Use the image attached to declare and call functions in Applab.
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

When we put a variable between parenthesis, what is it called?

1

In the following function printThreeTimes:

function printThreeTimes(word){
println(word);
println(word);
println(word);
}

What is the parameter of the function?

1

On which line is the "call" to the function doubleNumber on the video? (video: 4:50 )

1

What would I get if I called doubleNumber(10); ? (video: 5:01 )

1

After you watched all the video, Define the printNumbers function that takes three integers and print them in given order

function printNumbers(two, one, zero){
println(two);
println(one);
println(zero);
}


Call printNumbers function whenever printNumberBtn is clicked (hint:onEvent("printNumberBtn", "click", function(){ call functions here } );
var zero =0;
var one=1;
var two= 2;
printNumbers(zero, one, two);



What will be displayed on the screen?

1

Revise the printNumbers function to print given parameters in below order.

function printNumbers(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

“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

True or False: Functions must have parameters.

Function with Parameters 2
Watch the video above and please use your remixed project.
https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I


Watch the video above https://www.youtube.com/watch?v=obwNNtwh7uw until 2:20 then code along after.
https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I

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

How many parameters does the function sum use?

1

What would get printed if we added the code,
sum(4, 3); ?

Function with Return Values
1

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

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

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

Revise doubleNumber(x) function and call it inside doubleNumberBtn as shown below.

What is printed on the display?

1

List 3 things you have discovered today.