This exercise allows you to practice and apply the concepts and techniques taught in class.
Upon completion of this exercise, you will be able to:
- Determine correctly the variable scope in JavaScript code,
- Interpret correctly how hoisting affects the variables and functions in JavaScript code,
- Determine how variable shadowing affects the variables in JavaScript,
- Interpret correctly how primitive values are passed and compared in JavaScript,
- Interpret correctly how objects and arrays are passed and compared in JavaScript,
- Interpret correctly how function parameters behave in JavaScript,
Are you ready to put your coding skills to the test? In this exercise, you will be given a series of short code challenges that simulate the types of questions you may encounter during a Junior Developer job interviews or technical assessments. The challenges are designed to help you practice and reflect on the concepts of scope, hoisting, variable shadowing, and passing value versus passing reference. By examining the code and trying to predict the output, you will put your problem-solving skills to the test and gain a better understanding of how these concepts work in JavaScript.
- Fork this repo.
- Clone this repo.
- Upon completion, run the following commands:
git add .
git commit -m "Solved lab"
git push origin master
- Create a Pull Request and submit your assignment
We encourage you to try to solve the challenges without running the code first. Once you have written down your answer for a challenge, run the code and check if your answer was correct. If your answer was incorrect, try to understand why the code behaves the way it does.
You should record your answers in the answers.md
file and provide a brief explanation for each answer. For example:
1. Challenge 1:
- Answer: a
- Explanation: The output of the code will be "123" because...
// Challenge 1
let foo = "abc";
function bar() {
foo = "xyz";
console.log(foo);
}
bar();
console.log(foo);
Given the code above, answer the following questions:
- Which of the following will be the output of the above code? Why?
a)"abc"
and"xyz"
b)"xyz"
and"xyz"
c)undefined
and"xyz"
d) ReferenceError: foo is not defined
Once you write down your answer, run the code above and check if it is correct.
// Challenge 2
let a = 1;
function example(a) {
a = 10;
console.log(a); // Console log 1
}
example(a);
console.log(a); // Console log 2
Given the code above, without running the code, answer the following question:
- Which of the following will be the output of the above code? Why?
a)10
and10
b)1
and10
c)10
and1
d) ReferenceError: a is not defined
Once you write down your answer, run the code above and check if it is correct.
// Challenge 3
sayHi();
function sayHi() {
console.log("Hi there!");
}
Given the code above, without running the code, answer the following question:
- What will be the output of the code above? Why?
a)ReferenceError: sayHi is not defined
b)undefined
c)"Hi there!"
d)TypeError: sayHi is not a function
Once you write down your answer, run the code above and check if your answer was correct.
const a = { num: 42 };
const b = a;
b.num = 90;
console.log(a);
Given the code above, without running the code, answer the following question:
- What will be the output of the code above? Why?
a) TypeError: Assignment to constant variable.
b){ num: 42 }
c){ num: 90 }
d) ReferenceError: a is not defined
Once you write down your answer, run the code above and check if your answer was correct.
function magicHat(obj) {
obj.age = 10;
obj = { name: "Ada", age: 20 };
return obj;
}
const rabbit1 = { name: "Bob", age: 30 };
const rabbit2 = magicHat(rabbit1);
console.log("rabbit1: ", rabbit1);
console.log("rabbit2: ", rabbit2);
Given the code above, without running the code, answer the following question:
- What will be the output of the code above? Why?
a){ name: "Bob", age: 30 }
and{ name: "Ada", age: 20 }
b){ name: "Bob", age: 30 }
and{ name: "Bob", age: 10 }
c){ name: "Bob", age: 10 }
and{ name: "Ada", age: 20 }
d) ReferenceError: obj is not defined
Once you write down your answer, run the code above and check if your answer was correct.