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);
submitFinalGrade(grade) is returning a value. If a parameter is being passed in the calculateLetterGrade function,
Question 2

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

A local variable is assigned in the scope of a code block and shall only be applicable (called upon/referenced)
inside of that code block. A global variable can be referenced and changed anywhere
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: var totalStateTaxes, var totalFederalTaxes, var totalTaxes
Global: var stateTaxRate, var 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 variable sum (called upon in the alert after the function) is not initialized at a global scope.
So an error will come back that sum is not defined.
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 parseInt() function
Question 7

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

the parseFloat() function
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!");
}
@ line 102, firstName is now attempting to be reassigned instead of equality (==) checked .
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" while using debugger will execute an entire line or codeblock - taking you to the next
line of code (or function) while displaying the end results of the lines stepped over.
"Stepping into" a line of code will execute the line process by process, slowing down execution for
obervation.

Coding Problems

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