Title:
JavaScript Review Worksheet
Updated:
November 3, 2023
Task/Activity:
  1. Questions
  2. Coding Problems
A. Questions
  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);
    

calculateLetterGrade(), because in order for the code to work, that has to return the value that grade is defined as.

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

A local variable is a variable created inside of a function, and that only exists inside of that function, unable to be used anywhere else. Meanwhile, a global variable is a variable that is accessible anywhere inside of the code.

  1. 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;
    }
    
Local: totalFederalTaxes, totalStateTaxes, totalTaxes.
Global: stateTaxRate, federalTaxRate, (presumably)wages.
  1. 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);
                    
There is an attempt to use sum when it is not defined as a global variable.
  1. What function would you use to convert a string to an integer number?
parseInt()
  1. What function would you use to convert a string to a number that has a decimal in it (a 'float')?
parseFloat()
  1. 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!");
    }
    
It attempts to use an asignment statement as a conditional. (meaning the if statement needs two equal signs, not one.)
  1. 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);
    
20
  1. Explain the difference between truthy and falsy values in JavaScript. Provide a few examples of both truthy and falsy values.
A truthy variable has some value that isn't undefined, empty, or zero. A falsy variable is the opposite.
  1. One of the falsy values in JavaScript is NaN. What is NaN? When might you encounter a variable that is storing NaN?
NaN is 'Not a Number'. It occurs when you attempt to store something that isn't a number into a number variable.
  1. Explain the difference between the strict equality operator (===) and the equality operator (==).
Strict equality checks for equality and data type, whereas the equality just checks for equality.
  1. Explain the difference between stepping over and stepping into a line of code when using the debugger.
If you step into, you will operate the code from the exact point you are testing. If you step over, you will skip that line, going to the line directly after.
B. Coding Problems
  1. Coding Problems - See the 'script' tag at the bottom of the page. You will have to write some JavaScript code in it.