JavaScript Review
Question 1There 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);
Answer: The function `calculateLetterGrade(96)` returns a value because it assigns the result to the variable `grade`. The function `submitFinalGrade(grade)` does not return anything, it simply submits the grade.
Explain the difference between a local variable and a global variable.
Answer: A global variable is declared outside of any function and can be accessed anywhere in the code. A local variable is declared within a function and can only be used within that function.
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;
}
Answer: `stateTaxRate` and `federalTaxRate` are global variables because they are declared outside of any function. `totalStateTaxes`, `totalFederalTaxes`, and `totalTaxes` are local variables because they are declared within the `calculateTaxes` function.
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);
Answer: The problem is that `sum` is a local variable inside the function `addTwoNumbers`, so it is not accessible outside of the function. The `alert("The sum is " + sum)` will throw an error because `sum` is undefined in the global scope.
True or false - All user input defaults to being a string, even if the user enters a number.
Answer: True. In JavaScript, all user input from methods like `prompt()` is returned as a string, even if the input is a number.
What function would you use to convert a string to an integer number?
Answer: You can use `parseInt()` to convert a string to an integer.
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
Answer: You can use `parseFloat()` to convert a string to a floating-point number (decimal).
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!");
}
Answer: The problem is that the `=` operator is used instead of the `==` or `===` operators for comparison. The `=` operator is for assignment, not comparison. The correct code should be `if(firstName === "Bob")` to compare the value.
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);
Answer: The value of `x` will be 16. Here's the breakdown: - `x--` reduces `x` to 6 - `x += 3` increases `x` to 9 - `x++` increases `x` to 10 - `x *= 2` multiplies `x` by 2, resulting in 20 So, the final value is 20.
Explain the difference between stepping over and stepping into a line of code when using the debugger.
Answer: - Stepping over means executing the current line of code without going into any called functions. It will execute the code and move to the next line in the current function. - Stepping into means going inside the function being called on the current line. It allows you to debug the code inside the function one line at a time.