Ask your own question, for FREE!
Computer Science 10 Online
Ultrilliam:

Continuing where we left off: JavaScript lesson number 2

Ultrilliam:

All code on this post can be ran using Google Chrome Console. You can open the console like this: Windows: `Control + Shift + J` Mac: `Command + Option + J`

dude:

Now with mac support :')

Ultrilliam:

When we left off we were discussing Variables, Constants, Functions, and Datatypes This time we will be focusing on If/Else, Comparitors, Arrays, and JSON

Shadow:

This all says an hour ago. Is Jay typing out an essay?

Ultrilliam:

it should say 6 minutes ago....

Sam:

he made this post 13 mins ago shad

Ultrilliam:

Let's start with comparitors (wrong term for it, the correct term is operators) Comparing: > Greater Than >= Greater than or equal to < Less Than <= Less Than or equal to == Equal to === Equal to same data type != Does not equal !== Does not equal same data type && and || or ! not typeof Type of the input Operations: % Modulus, it divides and returns the remainder of the division + Addition, it adds both numbers, and combines strings together - It subtracts / Division * Multiplication ++ Increments a number -- Decrements a number = set's the value of something += Addition to the current value (or combining strings) -= Subtraction from the current value *= Multiplication from the current value /= Division from the current value %= Modulus from the current value

Ultrilliam:

Any questions thus far?

dude:

What is "data type"?

Ultrilliam:

Oh, and here's the link to the previous post for those who missed it https://questioncove.com/study#/updates/5a99be7a2117d6522969c000

Ultrilliam:

A data type is the type of data, be it string, integer, boolean, function, array, or JSON

Ultrilliam:

Any other questions?

ThisGirlPretty:

No sir

Ultrilliam:

To briefly explain a if statement: ```javascript if (conditon) { //code } ``` ```javascript if (conditon) { //code } else { //code } ``` ```javascript if (conditon) { //code } else if (condition) { //code } else { //code } ``` Alright, let's move onto actually putting those to use in examples Let's say we have this: ``` var x = 5; var y = 25; var z = x+y; //should be equal to 30 if (z == 30) { console.log('Evaled to true!'); } else { console.log('Evaled to false!'); } ```

Ultrilliam:

You should see `Evaled to true!` in the console

dude:

Got it

ThisGirlPretty:

Same!

BenLindquist:

nuts

Ultrilliam:

Let's now move onto a example of || and && ``` var op1 = 2+3 == 5; //Evaluates to true var op2 = 50+25 == 75; //Evaluates to true var op3 = 5+10 == 14; //Evaluates to false if (op1 && op2 || op3) { console.log('True!'); } ``` this code adds a bunch of numbers, and compares them, op1 and op2 are true, but op3 is false, yet the if statement still evaluates to true, this is because it asks if op1 AND op2 are true, OR if op3 is true so it get's through Make sense?

ThisGirlPretty:

Yes sir ^-^

dude:

Yes

Ultrilliam:

Using your knowledge from both lessons, I want you to write a function that contains a if statement, and returns the value of a operation if it evaluates to false, otherwise output something else. Any questions on these instructions?

ThisGirlPretty:

1 attachment
ThisGirlPretty:

Done. In you faceeee.

ThisGirlPretty:

Yos*

Ultrilliam:

Welp. You copy+pasted what I had, you didn't need to change the comment, and it's not a function

ThisGirlPretty:

Damn it.

dude:

dude:

>wait for ಠ_ಠ

ThisGirlPretty:

Looks right to me. A++++ ~o-o~

Ultrilliam:

It's not a function

Ultrilliam:

Shall I post a solution?

dude:

Yeh

ThisGirlPretty:

Ugh how do you make it not a function?

Ultrilliam:

``` function homework(hello,world) { var result = hello+world; if (result == 10) { return result; } else { return false; } } ```

Ultrilliam:

Did that clear things up?

ThisGirlPretty:

no, wut is homework for and 10

Ultrilliam:

homework is the name of the function 10 is just a random number

Ultrilliam:

you would run it and eval it to true by running `homework(5,5)`

ThisGirlPretty:

Oh okie

563blackghost:

is dat da solution to the homework you gave?

dude:

Yeh that is the solution

Ultrilliam:

A solution*

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!