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 is returning a value. The value it returns is being assigned to the variable grade.
Question 2

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

A local variable is declared withing a specific block of code, like a funtion, and is only able to be utilized within that black of code. It will not work outside of that code.

A global variable is declared outside of a funtion and can be used wherever, even within functions.
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;
}
  1. Local
    • wages
    • totalStateTaxes
    • totalFederalTaxes
    • totalTaxes
  2. Global
    • 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);
sum is a local variable and it not declared/initiated at the global level so the "alert("The sum is " + sum);" line will crash the program because the variable sum is not declared.
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 boolean expression in the if statement is assigning "Bob" to firstName rather than evaluating it. It should be (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);
It will log 20.
It reads as:
(((7 - 1) + 3) + 1) * 2
(((6) + 3) + 1) * 2
((9) + 1) * 2
(10) * 2
20
Question 10

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

  1. Step over
    This will execute the current line of code and move on to the next. This will execute a funtion and move on, NOT advised if attempting to actually debug a function.
  2. Step into
    This lets your move into the function to execute the funtion 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.