JavaScript Review

Question 1

There are two functions being called in the code sample below. Which one returns a value? How can you tell?

let grade = calculateLetterGrade(96);
            submitFinalGrade(grade);
let grade = calculateLetterGrade() returns a value. This is because the function call is being assigned to a (grade).
Question 2

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

Global variable is declared outside of any function, it can be accessed by all funtions in the script. A local variable is devlared inside a funtion, and can only be used within that function.
Question 3

Which variables in the code sample below are local, and which ones are global?

const stateTaxRate = 0.06;
const federalTaxRate = 0.11;

function calculateTaxes(wages){
	const totalStateTaxes = wages * stateTaxRate;
	const totalFederalTaxes = wages * federalTaxRate;
	const totalTaxes = totalStateTaxes + totalFederalTaxes;
	return totalTaxes;
}
The global variables in the sample are stateTaxRate, and federalTaxRate. The local variables in the sample are wages, totalStateTaxes, totalFederalTaxes and totalTaxes.
Question 4

What is the problem with this code (hint: this program will crash, explain why):

function addTwoNumbers(num1, num2){
	const sum = num1 + num2;
	alert(sum);
}

alert("The sum is " + sum);
addTwoNumbers(3,7);
The problem: the code tries to use the variable "sum" outside of the function where it's declared, "sum" is a local variable which should be inside the function.
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 would use ()Number.
Question 7

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

You use parseFloat().
Question 8

What is the problem with this code sample:

let firstName = prompt("Enter your first name");
if(firstName = "Bob"){
	alert("Hello Bob! That's a common first name!");
}
You need to use == instead of = for a comparison.
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)?

let x = 7;
x--;
x += 3;
x++;
x *= 2;
console.log(x);
Value of x = 8. It starts at 7 and increments it by 1.
Question 10

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

Stepping over means running the current line of the code and moving to the next line without going inside function calls. Stepping into means, if a current line calls a function, the debugger will allow you to see each step inside of that 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.