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);
Explain the difference between a local variable and a global variable.
Global variables are defined outside functions and can be called everywhere in the code.
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;
}
"totalStateTaxes", "totalFederalTaxes" and "totalTaxes" are local variables.
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);
True or false - All user input defaults to being a string, even if the user enters a number.
What function would you use to convert a string to an integer number?
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
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!");
}
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);
Explain the difference between stepping over and stepping into a line of code when using the debugger.