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);
Line one returns a value. It will return a letter grade (A or B or whatever the grade would be)
Question 2

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

Local varibles are nested within an if statement. While a golbal varible is determined before the statement and can be used anywhere in the code block.
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 varibles are are the 3 nested in the if statement and the global varibles are the fed and state tax rates in the first 2 lines.
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 varible of sum is inside the function, not outside. Meaning it is local. When you try to alert it, it wont work. It would have to be global.
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?

You can use parseInt
Question 7

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

You can use parseFloat
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!");
}
You have to set Bob == instead of =
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 will appear in the colsole log
Question 10

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

RStepping over allows the current line of code to be executed and move onto the next, while stepping into allows for you to dive into the line and see 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.