This exercise is designed to improve your understanding of functions, function arguments and return statements.
- Explain how functions, function arguments and
return
statements work
Go through each of the questions below, one by one, using the following process:
-
Write down a description of what you believe the function(s) are doing and what you think the answer to the question is. Do not skip the writing down step.
-
Run the code by typing (don't copy and paste, type the code for this exercise) it in to a js file and running the file with
node
. Name the file based on the question name (i.eq1.js
,q2.js
, etc.). Addconsole.log
statements to observe values where required. -
Did you answer correctly? If not, try to understand why not. What did you misunderstand about the code? This step is crucial to this exercise. If the answer is not what you expected, do not move on until you understand why. You can experiment with the code adding
console.log
messages to help you see what is happening. You can ask another member of the Cohort, you can look at previous exercises, online references, and of course you can ask the instructors using the support channel. -
If necessary update your written answer (keep the original!) with your updated understanding.
-
Commit and push your updates
-
Move on to the next question.
Look at the example-question.md
file for an example of what your writeup should look like (you don't need to include the actual code in your own write up, it's just provided in the example for clarity).
At the end of all questions, in a new MD file describe in your own words:
- What a function is
- How function arguments work
- How return statements work
Share this final write up with your instructor.
This process is an example of applying a learning cycle. Remember this diagram from the beginning of the course?
Be conscious of this process as you go through the exercise. If the code for a particular question does something you didn't expect, ask yourself why. Modify the code as necessary to help understand it's behavior, copy the code to a new file, add console.log messages to give you visibility. This process is the key to developing your own understanding of how specific concepts work. Keep this diagram in mind as you go through the exercise.
Your instructor will demonstrate this process for the first question.
What is the value of result
after calling this function? Why?
function myFunction(num1, num2) {
return num1+num2
}
const result = myFunction(5,5)
What is the value of result
after calling this function? Why?
function myFunction(num1, num2) {
num1+num2
}
const result = myFunction(5,5)
What is the value of num
at the end of the program? Why?
function myFunction(num) {
return num-1
}
let num = 10
num = myFunction(num)
num = myFunction(num)
What is the value of add
and num
at the end of the program? Why?
function myFunction(num) {
return num-1
}
let num = 10
let add = 3
add = myFunction(add)
add = myFunction(add)
What value will be logged inside the function call? Why?
function myFunction(num, num1) {
console.log(num1)
}
let num = 10
let num1 = 2
myFunction(num)
What value will be logged inside the function call? Why?
function myFunction(num, num1) {
console.log(num1)
}
let num = 10
let num1 = 2
myFunction(num1, num)
What will the value of counter be at the end of this program? Why?
let counter = 1
function myFunction() {
counter++
return counter
}
myFunction()
const num = myFunction()
What will the value of result
be at the end of this program? Why?
function myFunction(num1, num2) {
return num1 + num2
}
const num1 = 10
const num2 = 1
const num3 = 4
const result = myFunction(num3, num1)
What will be logged out on the console when this code rus? Why?
function myFunction(num1, num2) {
console.log(num3)
}
const num1 = 10
const num2 = 1
const num3 = 20
myFunction(num3, num1)
What will be logged out on the console when this code runs? Why?
function myFunction(num1, num2, num3) {
console.log(num3)
}
const num1 = 10
const num2 = 1
const num3 = 20
myFunction(num3, num1, 100)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2, num3) {
return num1 + num2 + num3
}
const num1 = 10
const num2 = 1
const num3 = 20
const result = myFunction(1, 1, 1)
What will be the value of result
when this code runs? Why?
function getSomeValue() {
return 2
}
function myFunction(num1) {
const num2 = getSomeValue()
return num1 * num2
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function getSomeValue() {
return 2
}
function myFunction(num1) {
const num2 = getSomeValue()
return num1 * getSomeValue()
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function getSomeValue() {
return 2
}
function myFunction(num1) {
return getSomeValue() * getSomeValue()
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function myFunction(num1) {
if(true) {
return -10
}
return num1 * 10
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function myFunction(num1) {
if(false) {
return -100
}
return num1 * 10
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function myFunction(num1) {
return -100
return num1 * 10
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function myFunction(num1) {
return num1 * 10
return -100
}
const result = myFunction(5)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2, num3) {
return num2
}
const result = myFunction(5, 10, 15)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2, num3) {
return num1 + num3
}
const num1 = 20
const num2 = 200
const num3 = 1000
const result = myFunction(5, 10, num3, 15)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2) {
const result = num1+num2
return result
}
const result = myFunction(10, 20)
myFunction(100, 2)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2) {
let result = num1+num2
return result
}
let result = 0
myFunction(100, 2)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2) {
result = num1+num2
}
let result = 0
myFunction(100, 2)
What will be the value of result
when this code runs? Why?
function myFunction(num1, num2) {
const result = num1+num2
return 100
}
const result = myFunction(5, 2)
What will be the printed out by the console log statements when this code runs? Why?
function myFunction(a) {
let b = 20
console.log("a:", a)
console.log("b:", b)
console.log("c:", c)
}
let a = 1
let b = 2
let c = 3
myFunction(100)