Links to lesson slides – Why is ScriptEd here? – Review of JS Variables and Functions



Links to lesson slides – Why is ScriptEd here? – Review of JS Variables and Functions

0 0


ScriptEd_lesson_slides

A repo for slides for lessons for ScriptEd classes

On Github edgenard / ScriptEd_lesson_slides

JavaScript Review

Variables and Functions

But First...

Why is ScriptEd here?

We want to introduce you to the technology industry

We are also looking for interns

A tech company in NYC getting experience doing real work.

Getting paid.

We have to make sure you are ready to work for a company

Know enough that you can contribute meaningfully to the company

Be able work on your own

Be able to work on a team

Ways we are going to help do that:

Finish eating by 4:20

No cellphone use while in class. If this is a problem we will offer to hold it for you till the end of class.

Listening to music is only OK if you are

  • Working by yourself
  • Have Headphones.

Variables

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

var number = 1;
number = 1;
number 1;

Which of these is valid variable names?

var 3six;
var six3;
var *3six;
var $3six;
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

Write a function that takes in a string and prints out string with with "Hello, " in front of it.

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.

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.

You Do!

Write a function that calculates the days left until Christmas. HINT: Look up Dates in JavaScript

if/else if/else

Sometimes you only want the computer to do something if something else is true.

What will this print to the console

if(5 > 4){
	console.log("Yes");
} else {
	console.log("NO");
}

What will this print to the console?

if(10 > 11){
	console.log("10 is greater than 11");
}else{
	console.log("10 is not greater than 11")
}

Boolean Operators

 > 
means greater than

 < 
means less than

 === 
means equal to

So Does

==
But always use three equal signs

More Boolean Operators

!==
means not equal to

Putting a

!
in front of any means its opposite

&&
means AND

||
means OR

What does this print out

if(!(5 > 4)){
  console.log("Yes");
}else {
  console.log("NO");
}

What does this print out?

if( 5 > 4 && 6 > 5){
  console.log("Both are true");
}else{
  console.log("Both were not true");
}

What does this print out?

if(5 > 4 || 3 > 4){
  console.log("At least one was true");
}else{
  console.log("None were true");
}

What does this print out?

if( 5 !== 4){
  console.log("That's right 5 is not equal to 4")
}else {
  console.loge("5 is equal to 4")
}

Your Turn!

  • A function called "max" that takes in two numbers and returns larger of the two.
  • A function called "maxOfThree()" that takes three numbers and returns the largest of the three.
  • A function called "longestWord()" that takes two words and returns the longest word? Hint: How do you find out how long a string is in JavaScript?
JavaScript Review Variables and Functions