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);
From what I can tell, "calculateLetterGrade()" is the one which returns a value. That is because it is expected to give an output based off of another value, whereas "submitFinalGrade()" is only bringing the value to somewhere else.
Question 2

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

Global variables affect the entire script, whereas locals are only used in a specific function. Provided you use proper syntax and organization, you can tell the difference because globals are provided on their own, whereas locals are children of functions.
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;
}
stateTaxRate and federalTaxRate are global. The rest are local.
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);
Nowhere is num1 nor num2 given a value. The function cannot work unless these two are identified.
Question 5

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

False. That is only the case if it is contained within quotation marks.
Question 6

What function would you use to convert a string to an integer number?

That would be "parseInt()".
Question 7

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

And that would be "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!");
}
The if boolean uses an assignment operator (=) when it should use an equality operator (==). This won't work as is since it will just change firstName to Bob regardless of what was previously entered.
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);
In the end, x should become 20.
Question 10

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

When you "step into" a line of code, you're not only executing the code but also examining the functions within it that are invoked and seeing their structure. If you wish to only run the code without the examination, you'd "step over" the code.

Coding Problems

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