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 two functions being called are calculateLetterGrade and submitFinalGrade. The function that returns a value would be the calculateLetterGrade(96). Why is
because you're passing your grade in the parameter which then takes that input, processes it and then returns a result which is then stored with the grade
variable.
Question 2
Explain the difference between a local variable and a global variable.
Global variables are variables that are put outside the functions (usually at the top of page) and can be called anywhere at any time. Local variables are
variables used ONLY within a function and can't be called outside to any other function or statement.
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;
}
Your global variable would be federalTaxRate and I can tell because it's put outside the function and also at the top of the page. The local variable would be
totalStateTaxes, totalFederalTaxes, and totalTaxes. Those are local because they are within the function and can only be used within that function.
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 this one is that the alert("The sum is " + sum) is trying to call the sum variable but it can't because it's inside the function meanign its a
local variable. To fix this you can either add that alert line of code with the already built in alert code thats inside the function or you can add a return
function within the function after the const. Then store the new return value in a new variable outsude the function and then using the alert function again.
Question 5
True or false - All user input defaults to being a string, even if the user enters a number.
That is true.
Question 6
What function would you use to convert a string to an integer number?
You would use the parseInt() function. An example would be like this,
const number = parseInt(prompt("Enter a number"));
Question 7
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
You would 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 this one is that when setting the parameter in the if statement there is only one equal sign when you need two. You need two because having ones
is the assignment operator and having two is the equal operator.
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 answer will be 20.
Question 10
Explain the difference between stepping over and stepping into a line of code when using the debugger.
When stepping it takes us to the next line of code and when stepping into, it will take us into that line of code if its a function and shows us how each line of
code is being executed.
Coding Problems
Coding Problems - See the 'script' tag at the bottom of the page. You will have to write some JavaScript code in it.