JavaScript Review
Question 1
There are two functions being called in the code sample below.
Which one returns a value? How can you tell?
var grade = calculateLetterGrade(96);
submitFinalGrade(grade);
The line of code that states var grade = calculateLetterGrade(96); will return a value.
In this case 'grade' is storing the the value 'calculateLetterGrade(96)'. In the case of 'submitFinalGrade' the function is not complete, so it will not return a value.
Question 2
Explain the difference between a local variable and a global variable.
The main difference between a local variable and a global variable
is that a 'local variable' is declared and accessed within a function and 'global variables'
are variables that are declared outside a function. To clarify local variables are declared with 'let', 'var', or 'const' etc
and generally confined within the scope of a function that defines them. Global variables on the other hand, are the variable outside the
scope of a function and any variable declared without the local keywords become global variables.
Question 3
Which variables in the code sample below are local, and which ones are global?
var stateTaxRate = 0.06;
var federalTaxRate = 0.11;
function calculateTaxes(wages){
var totalStateTaxes = wages * stateTaxRate;
var totalFederalTaxes = wages * federalTaxRate;
var totalTaxes = totalStateTaxes + totalFederalTaxes;
return totalTaxes;
}
In this function 'stateTaxRate' and 'federalTaxRate' are global variables and 'totalStateTaxes',
'totalFederalTaxes', and 'totaltaxes' are local variables.
Question 4
What is the problem with this code (hint: this program will crash, explain why):
function addTwoNumbers(num1, num2){
var sum = num1 + num2;
alert(sum);
}
alert("The sum is " + sum);
addTwoNumbers(3,7);
This code will crash because sum has not been defined anywhere, there is an error because there is no connection
between addTwoNumbers and sum defined within the function.
Question 5
True or false - All user input defaults to being a string, even if the user enters a number.
True, unless the variable is defined as a number the function defaults as a 'string'.
Question 6
What function would you use to convert a string to an integer number?
A 'Number()' method turns a value into a number in a function. It needs to be declared as a number to be an integer.
Question 7
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
In JavaScript you can use the parseFloat() to make a string into a number with a decimal in it.
Question 8
What is the problem with this code sample:
var firstName = prompt("Enter your first name");
if(firstName = "Bob"){
alert("Hello Bob! That's a common first name!");
}
The issue with this function is the statement that is stating 'Bob' as the first name
in instead of using '=' we should be using '==' to set first name equal to 'Bob'.
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)?
var x = 7;
x--;
x += 3;
x++;
x *= 2;
console.log(x);
6, 10, 8, 14
Question 10
Explain the difference between stepping over and stepping into a line of code when using the debugger.
When you step over a line of code in debugger it just moves on to the next line of code,
while stepping in actually opens the function to see the details of 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.