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);
When declaring this function, it was set to calculate the letter grade and return the value of that calculation. When executing the function, a value for the parameter is passed in, and the value of the calculation is stored.
Explain the difference between a local variable and a global variable.
A global variable is a variable that is defined outside of a function or code block. This variable is accessible for use at any point when writing the 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;
}
Global Variables: stateTaxRate, federalTaxRate
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);
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:
var firstName = prompt("Enter your first name");
if(firstName = "Bob"){
alert("Hello Bob! That's a common first name!");
}
For example: if(firstName == "Bob")
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);
Explain the difference between stepping over and stepping into a line of code when using the debugger.
When stepping into a line of code, you can observe the code that runs inside the body of a function. This allows you to go line-by-line within the function and observe how it executes in further detail. A function can be stepped over, instead of stepped into, and the browser will quickly execute all of the lines of cude in the function body, and the debugger will move to the next line down.