JavaScript Review
Question 1
There are two functions being called in the code sample below.
Which one returns a value? How can you tell?
let grade = calculateLetterGrade(96);
submitFinalGrade(grade);
The function calculateLetterGrade(96) returns a value, which is assigned to the variable grade. You can tell because its result is being stored in a variable. The submitFinalGrade(grade) function does not return a value; it is likely performing an action (like submitting the grade) without returning anything.
Question 2
Explain the difference between a local variable and a global variable.
A local variable is declared within a function and can only be accessed inside that function. A global variable is declared outside of any function and can be accessed from anywhere in the code.
Question 3
Which variables in the code sample below are local, and which ones are global?
const stateTaxRate = 0.06;
const federalTaxRate = 0.11;
function calculateTaxes(wages){
const totalStateTaxes = wages * stateTaxRate;
const totalFederalTaxes = wages * federalTaxRate;
const totalTaxes = totalStateTaxes + totalFederalTaxes;
return totalTaxes;
}
The variables stateTaxRate and federalTaxRate are global because they are declared outside the function. The variables totalStateTaxes, totalFederalTaxes, and totalTaxes are local because they are declared inside the function calculateTaxes.
Question 4
What is the problem with this code (hint: this program will crash, explain why):
function addTwoNumbers(num1, num2){
const sum = num1 + num2;
alert(sum);
}
alert("The sum is " + sum);
addTwoNumbers(3,7);
The problem with this code is that the variable sum is declared inside the function addTwoNumbers, so it is local to that function and cannot be accessed outside of it. The alert outside the function tries to use sum, but sum is not defined in that scope, causing the program to crash.
Question 5
True or false - All user input defaults to being a string, even if the user enters a number.
True. All user input from functions like prompt() is received as a string, even if the user enters a number.
Question 6
What function would you use to convert a string to an integer number?
You would use the parseInt() function to convert a string to an integer number.
Question 7
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
You would use the parseFloat() function to convert a string to a floating-point number.
Question 8
What is the problem with this code sample:
let firstName = prompt("Enter your first name");
if(firstName = "Bob"){
alert("Hello Bob! That's a common first name!");
}
The problem with this code is that the single equals sign (=) is used for assignment, not comparison. To compare values, you should use the double equals (==) or triple equals (===) operator. The corrected code should be:
if(firstName === "Bob") {
alert("Hello Bob! That's a common first name!");
}
Question 9
What will the value of x be after the following code executes (in other words, what will appear in the log when the last line executes)?
let x = 7;
x--;
x += 3;
x++;
x *= 2;
console.log(x);
The value of x will be 20. Here's the breakdown:
- Start with x = 7
- x-- decreases x by 1, so x = 6
- x += 3 adds 3 to x, so x = 9
- x++ increases x by 1, so x = 10
- x *= 2 multiplies x by 2, so x = 20
Therefore, when console.log(x) is executed, it will log 20.
Question 10
Explain the difference between stepping over and stepping into a line of code when using the debugger.
Stepping over a line of code means executing the entire line without going into any functions that the line might call. Stepping into a line of code means entering into any function calls on that line and pausing at the first line inside the function, allowing you to debug the function's code step-by-step.
Coding Problems
Coding Problems - See the 'script' tag at the bottom of the page. You will have to write some JavaScript code in it.