JavaScript Review

Variables and Functions

Variables

Which of these is the correct way to declare a variable?

  1. 
    var number = 1;
    
  2. 
      number = 1;
    								
  3. 
      number 1;
    								

Which of these is valid variable names?

  1. 
    								 var 3six;
    								
  2. 
    									var six3;
    								
  3. 
    									var *3six;
    								
  4. 
    								var $3six;
    							
  5. 
    									var _3six;
    								

Variables Names in JavaScript

Can start with

$
_
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

Can include numbers after the first character

var six3 = 63;

Cannot be one of the special words

break

default

function

return

var

case

delete

if

switch

void

catch

do

in

this

while

const

else

instanceof

throw

with

continue

finally

let

try

debugger

for

new

typeof

Functions

How to write a function


								function nameOfFunction
							

								function add3(arg)
							
							
function add3(num){
	return num + 3;
}
							
						

Write your own functions

Write a function that takes in a number and prints out the number multiplied by 5

JS Bin on jsbin.com

Write a function that takes in a string and prints out string with with "Hello, " in front of it. JS Bin on jsbin.com

Write a function that takes in 2 numbers where the first number is the base and the second number is the exponent. Print out the value of raising the base by the exponent.

JS Bin on jsbin.com

Write a function that take in 2 numbers, divides the first number by the second number and prints out the answer rounded to the nearest integer.

JS Bin on jsbin.com

You Do!

Write a function that calculates the days left until Christmas.

Hints

  1. First figure out how to get a specific Date in JavaScript. Javascript start counting months at 0 so January is 0 and December is 11
  2. You will have to subtract the date at Xmas from today
  3. The answer you get back will be in milliseconds
  4. How do you turn that into days?