JavaScript Review

Question 1

There 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);
The function calculateLetterGrade is returning a value.

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.
Question 2

Explain the difference between a local variable and a global variable.

A local variable is a variable that is defined within a function or code block. This variable can only be used within that function.

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.
Question 3

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;
}
Local Variables: totalStateTaxes, totalFederalTaxes, totalTaxes
Global Variables: stateTaxRate, federalTaxRate
Question 4

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 alert outside of the function is trying to use the local variable sum, but since sum was declared within the function addTwoNumbers, it cannot be used outside of that specific function.
Question 5

True or false - All user input defaults to being a string, even if the user enters a number.

True
Question 6

What function would you use to convert a string to an integer number?

The function parseInt() would need to be utilized to convert a string to an interger number. Using this function, if a user enters a number, the data type would remain a number and would not be the default of string.
Question 7

What function would you use to convert a string to a number that has a decimal in it (a 'float')?

The function parseFloat() would need to be utilized to convert a string to a number with a decimal in it (also known as a 'float').
Question 8

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!");
}
In the IF statement, firstName is being incorrectly assigned to the string of "Bob". Instead of using the assignment operator (=), the equality operator (==) should be used instead.

For example: if(firstName == "Bob")
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)?

var x = 7;
x--;
x += 3;
x++;
x *= 2;
console.log(x);
20
Question 10

Explain the difference between stepping over and stepping into a line of code when using the debugger.

When using the debugger, stepping over a line of code, it executes that one line of code.

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.

Coding Problems

Coding Problems - See the 'script' tag at the bottom of the page. You will have to write some JavaScript code in it.