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);
The function calculateLetterGrade(96) returns a value, while submitFinalGrade(grade) does not.
I can tell because calculateLetterGrade(96): This function is assigned to the variable grade, which indicates that it returns a value.
Question 2

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

A local variable is declared inside a function and can only be used within that function.
A global variable is declared outside any function and can be accessed anywhere in the code.
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;
}
Global variables: stateTaxRate, federalTaxRate because it is declared outside
Local variables: totalStateTaxes, totalFederalTaxes, totalTaxes because it is declared inside
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 with the code is that the second alert tries to use the sum variable outside the function, but sum is only defined inside the addTwoNumbers function. This means sum doesn't exist outside the function, and the code will give an error.
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?

To convert a string to an integer we can use 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')?

To convert a string to a number with a decimal (a float) in JavaScript, we can use the parseFloat() function.
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!");
}
The problem with the code is that the if condition is using a single equals sign (=), instead of the double equals (==) or triple equals (===).
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);
x will be 20.
Question 10

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

Stepping Over skips over functions and moves to the next line.
Stepping Into lets you go inside a function to see its details.

Coding Problems

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