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);
calculateLetterGrade() returns a value because it has a variable "grade" attached to it that captures the value thanks to the assignment operator "=".
Question 2

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

A local variable exists only within a block of code/function where it is initiated/defined. It will be undefined if it is called anywhere else in the code. A global variable has global significance, meaning once it is defined in the code it is usable within any codeblock or function. The global and local signifcance is determined by where the variable is defined in 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;
}
Global: stateTaxRate, federalTaxRate
Local: totalStateTaxes, totalFederalTaxes, totalTaxes
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 problem with the code is that the variable "sum" is a local variable defined within the function "addTwoNumbers", but it is being called in the alert() function, which is outside the local scope that "sum" exists in.
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?

parseInt()
Question 7

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

Number()
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!");
}
The if() function needs to have "==" because the way it is written now it is trying to reassign the firstName variable rather than check for equality using "==".
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.

Stepping over a line of code will execute the code and move onto the next line. Stepping into a line of code will allow the user to step inside any functions that are executing in that line and see the code within the function.

Coding Problems

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