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)," is the one that returns a value. We know this because the output is assigned the variable of "grade," so we know that it cannot return a value if it is the output variable.
Question 2

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

A local variable is declared within a function or block and can only be used inside that function or block. Whereas, a global variable is declared outside of a function or block making it accessible 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;
}
The global variables are "const stateTaxRate" and "const federalTaxRate." You can tell because they are outside of a function or block.
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 issue with this line of code is that the "sum" variable is declared only in the function, so outside of it the "sum" will not work. To fix this, you would need to move the "sum" variable outside of the function.

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

                    addTwoNumbers(3, 7);
                    alert("The sum is " + sum);
                 
Question 5

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

It is true. When end-users enter an input, it is automatically a string by default. Even if a number is written, the program will see it as text. You will need to convert the number using a line of code such as, "parseInt()" or "parseFloat()."
Question 6

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

As referenced in problem #5, you would use a function to convert the string to a number. For this situation, you would use "parseInt()."
Question 7

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

As referenced in problem #5, you would use a function to convert the string to a number. For this situation, you would 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!");
}
The issue with this line of code is that the "=" is not the correct comparision operator. Basically, the "=" is the operator for the assignment-operator, meaning that the name in fact will always be "Bob" so the if-statement will always execute regardless of what anyone inputs in the prompt. To fix this, you need to use the comparison operator.

                    let firstName = prompt("Enter your first name");
                    
                    if (firstName == "Bob"){
                    alert("Hello Bob! That's a common first name!");
                    }
                 
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);
The console.log(x) will show a value of "20."
Question 10

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

Step-over means that the line of code will be executed but skip any functions called, and move onto the next line of code. Step-into runs the line of code and any functions called to debug the inner workings.

Coding Problems

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