0
0
Javascriptprogramming~10 mins

Function scope in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable inside the function scope.

Javascript
function greet() {
  let [1] = 'Hello';
  console.log(greeting);
}
greet();
Drag options to blanks, or click blank then click option'
AsayHello
Bmessage
Cgreet
Dgreeting
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a variable name different from the one logged.
Declaring the variable outside the function.
2fill in blank
medium

Complete the code to access a variable declared outside the function.

Javascript
let name = 'Alice';
function sayName() {
  console.log([1]);
}
sayName();
Drag options to blanks, or click blank then click option'
Aname
BName
Cuser
Dusername
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a variable name with wrong capitalization.
Trying to access a variable not declared in scope.
3fill in blank
hard

Fix the error in the code by completing the blank with the correct variable name.

Javascript
function test() {
  let score = 10;
  if (true) {
    let score = 20;
    console.log([1]);
  }
  console.log(score);
}
test();
Drag options to blanks, or click blank then click option'
Ascore
BScore
Cscor
Dscores
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using incorrect capitalization.
Trying to access a variable that does not exist.
4fill in blank
hard

Fill both blanks to create a function that returns the sum of two numbers.

Javascript
function add([1], [2]) {
  return a + b;
}
console.log(add(3, 4));
Drag options to blanks, or click blank then click option'
Aa
Bb
Cx
Dy
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using parameter names different from those in the return statement.
Swapping the order of parameters.
5fill in blank
hard

Fill all three blanks to create a function that updates a variable inside its scope and logs it.

Javascript
function update() {
  let count = 0;
  count [1] 5;
  console.log([2]);
  return [3];
}
update();
Drag options to blanks, or click blank then click option'
A+=
Bcount
D-=
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the wrong operator like -=.
Logging or returning a variable not declared.