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);
That would be calculateLetterGrade because it is assigned as having a 96.
there for when grade is called it will return whatever calculateLetterGrade is.
Question 2

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

local only works in a specific lines of the code.
Like if I made a variable in an if function I can't call it back unless it is within the if function
Global works in all lines of code and can be recalled
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: stateTaxRate, federalTaxRate
Global: 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);
Because it is not returning a sum after the function is being called
Question 5

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

False
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')?

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!");
}
it needs a double equal sign otherwise it'll look for a variable name 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. Math bellow
x = 7 (equals our starting number being 7)
x--; (equal to x - 1)
x += 3; (equal to x + 3)
x++; (equal to x + 1)
x *= 2; (equal to x * 2)

inshort the math would be
7 - 1 + 3 + 1 * 2 = (anwser)
which would mean by the end x = 20
Question 10

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

stepping over is just running each code line by line
stepping into means it will jump back up if a function is being call and run the line of code before returning to doing it line by line

Coding Problems

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