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 calculateLetterGrade(96) function gives back a value, which is then put in the variable grade. You can tell because it’s assigned to grade, meaning it returns something.
The submitFinalGrade(grade); function doesn’t return anything, just does some action, since it’s not saved to a variable.
Question 2

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

A local variable is a variable made inside a function or block and only works there. When the function is done running, the local variable is gone.
A global variable is made outside of any function or block, so it can be used anywhere in the code. It stays around as long as the program runs.
Question 3

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

let stateTaxRate = 0.06;
var federalTaxRate = 0.11;

function calculateTaxes(wages){
	var totalStateTaxes = wages * stateTaxRate;
	var totalFederalTaxes = wages * federalTaxRate;
	var totalTaxes = totalStateTaxes + totalFederalTaxes;
	return totalTaxes;
}
Global variables:
stateTaxRate, federalTaxRate, These variables are defined outside of any function, so they can be accessed from anywhere in the code.
Local variables:
totalStateTaxes, totalFederalTaxes, totalTaxes, These variables are defined inside the calculateTaxes function, so they can only be used within this function and are not accessible outside of it.
Question 4

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

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

alert("The sum is " + sum);
addTwoNumbers(3,7);
The problem is that sum is only available inside the function addTwoNumbers. When you try to use sum outside the function in alert("The sum is " + sum);, the program crashes because sum doesn’t exist there.
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 in JavaScript, you 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 decimal (or "float") in JavaScript, you 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 = sign sets firstName to "Bob" instead of checking if it’s "Bob". So the alert will always show.
To fix it, use == or ===
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);
20
Question 10

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

Step into goes inside a function to show you each line of code inside it. Use this if you want to see exactly what happens inside the function.
Step over skips over the function and moves to the next line of code in the main part. Use this if you don’t need to see what’s happening inside the function and just want to keep going.

Coding Problems

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