JavaScript Review
Question 1There are two functions being called in the code sample below. Which one returns a value? How can you tell?
var grade = calculateLetterGrade(96);
submitFinalGrade(grade);
"calculateLetterGrade(96)"" because it's giving us a result that is stored in grade
Explain the difference between a local variable and a global variable.
Local variblaes are declared inside a function or a blcok of code, while global varibles is declared outside of the function or blaokc of code
Which variables in the code sample below are local, and which ones are global?
var stateTaxRate = 0.06;
var federalTaxRate = 0.11;
function calculateTaxes(wages){
var totalStateTaxes = wages * stateTaxRate;
var totalFederalTaxes = wages * federalTaxRate;
var totalTaxes = totalStateTaxes + totalFederalTaxes;
return totalTaxes;
}
"stateTaxRate" and federalTaxRate" global Everything in the "funtion calculateTaxes" are local
What is the problem with this code (hint: this program will crash, explain why):
function addTwoNumbers(num1, num2){
var sum = num1 + num2;
alert(sum);
}
alert("The sum is " + sum);
addTwoNumbers(3,7);
The "sum" is a local variavle within the "addTwoNumbrs" function
True or false - All user input defaults to being a string, even if the user enters a number.
True
What function would you use to convert a string to an integer number?
The "parseInt" function
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
The "parseFloat" funtion
What is the problem with this code sample:
var firstName = prompt("Enter your first name");
if(firstName = "Bob"){
alert("Hello Bob! That's a common first name!");
}
The problem is in the "if". It needs to be ==.
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)?
var x = 7;
x--;
x += 3;
x++;
x *= 2;
console.log(x);
20
Explain the difference between stepping over and stepping into a line of code when using the debugger.
Stepping over is when you move to the next line of code wihtout going inside any funiton. Stepping into is when you go into the funtion to see every step that is taken.