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 function 'calculateLetterGrade' is returning a value. This is because the variable 'grade' is expecting a value to be returned to it after the function has been invoked. While with the 'submitFinalGrade' function it is using the 'grade' as a parameter and to our knoweldge does not need to return a value.
Question 2
Explain the difference between a local variable and a global variable.
A 'local variable' is only accessable by the function that it was declared in. 'Global variable' is declared outside of a function and is able to be used by any function or even called on its own.
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;
}
For the example above, the variables of 'stateTaxRate' and 'federalTaxRate' are both gobal variables due to being declared outside of a function code block. While the variable "totalTaxes" is a local variable because it was called inside of the function.
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);
In this example, we are trying to call a variable that is a local variable. If we wanted to use this we would have to make a global variable that is outside the function that is called 'sum' and have it equal to the function 'addTwoNumbers'. Something like: function addTwoNumbers(num1, num2){return num1 + num2;} let sum = 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.
Yes, all user input is considered a string. To change this we would have to use parseInt and then it would change to a number.
Question 6
What function would you use to convert a string to an integer number?
As mentioned before it is parseInt.
Question 7
What function would you use to convert a string to a number that has a decimal in it (a 'float')?
For number with a decimal we would use the parseFloat to have it change from a string to a number.
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!");
}
When using the 'if' statement and wanting to compare values they should have used the == and not =; = is used for initializing a variable or assiging a new value to a variable that already was initialized. == (equality operator) should be used to comapre to values to see if they are equal.
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);
The value for x should be 20. We started off with telling the code the x = 7, the enxt command was to minus 1 by using a decrement operator (This gave us 6). We then added 3 using short-cut operator (9), followed by and increment increase of 1 (This lead us to 10). Finally, we times that by 2 which gave us 20.
Question 10
Explain the difference between stepping over and stepping into a line of code when using the debugger.
'Stepping Over' is going to a line of code and excuting all that line but not going into the function, so you don't know if something went wrong in that function or what line. While 'Stepping in' is simlair to 'Stepping over' expect it will go into function and pauses at the first line of the function so you can see if something is wrong with that function as you debug 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.