js 3 fizzbuzz
This commit is contained in:
parent
cedce54eef
commit
6d38e1e7b1
@ -1,4 +1,5 @@
|
|||||||
###Javascript Variables, Lists and Objects
|
Javascript Variables, Lists, and Objects
|
||||||
|
========================================
|
||||||
|
|
||||||
Create a variable named byte and set it equal to "school".
|
Create a variable named byte and set it equal to "school".
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
###Javascript Objects In Depth
|
Javascript Objects In Depth
|
||||||
|
===========================
|
||||||
|
|
||||||
Model these people as objects:
|
Model these people as objects:
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
####Conditionals and For Loops
|
Conditionals and For Loops
|
||||||
|
==========================
|
||||||
|
|
||||||
Get ready for FizzBuzz!
|
Get ready for FizzBuzz!
|
||||||
|
|
||||||
@ -6,12 +7,12 @@ This is a classic programming test - that yes, you will actually see at job inte
|
|||||||
|
|
||||||
It basically just checks if you can write a for loop and if you know what [modulo](http://en.wikipedia.org/wiki/Modulo_operation) is.
|
It basically just checks if you can write a for loop and if you know what [modulo](http://en.wikipedia.org/wiki/Modulo_operation) is.
|
||||||
|
|
||||||
###For Loop
|
#### For Loop
|
||||||
|
|
||||||
This is the most basic loop we write in Javascript, and most languages for that matter. The ubiquitous For Loop.
|
This is the most basic loop we write in Javascript, and most languages for that matter. The ubiquitous For Loop.
|
||||||
|
```
|
||||||
for(var i = 0; i<=100; i++){ console.log(x) }
|
for(var i = 0; i<=100; i++){ console.log(x) }
|
||||||
|
```
|
||||||
In the first argument, we declare an incrementing variable, i, and where it will start. We could have called it anything.
|
In the first argument, we declare an incrementing variable, i, and where it will start. We could have called it anything.
|
||||||
|
|
||||||
In the second argument, we state the conditions under which the following code should be executing. In this case, it is while i is less than or equal to 100.
|
In the second argument, we state the conditions under which the following code should be executing. In this case, it is while i is less than or equal to 100.
|
||||||
@ -19,31 +20,31 @@ In the second argument, we state the conditions under which the following code s
|
|||||||
In the third argument, we say that every time the code is run, i should increment by 1. i++ is shorthand, also known as [syntactic sugar](http://en.wikipedia.org/wiki/Syntactic_sugar) for i += 1, or i = i + 1. They all do the same thing.
|
In the third argument, we say that every time the code is run, i should increment by 1. i++ is shorthand, also known as [syntactic sugar](http://en.wikipedia.org/wiki/Syntactic_sugar) for i += 1, or i = i + 1. They all do the same thing.
|
||||||
|
|
||||||
To get a slightly different view of it, we could do it this way.
|
To get a slightly different view of it, we could do it this way.
|
||||||
|
```
|
||||||
for(var x = 500; x > 150; x-=5){ console.log(x) }
|
for(var x = 500; x > 150; x-=5){ console.log(x) }
|
||||||
|
```
|
||||||
Try it in your browser or node console and watch it work. What is the last number it prints? Why?
|
Try it in your browser or node console and watch it work. What is the last number it prints? Why?
|
||||||
|
|
||||||
###Conditionals - If / Else
|
#### Conditionals - If / Else
|
||||||
|
|
||||||
The most simple conditional statement is the if statement. This is also ubiquitous across many programming languages.
|
The most simple conditional statement is the if statement. This is also ubiquitous across many programming languages.
|
||||||
|
|
||||||
Basically it is written like this:
|
Basically it is written like this:
|
||||||
|
```
|
||||||
if(some condition){
|
if(some condition){
|
||||||
do this
|
do this
|
||||||
}
|
}
|
||||||
|
```
|
||||||
You can make it more powerful by specifying an else. This is a catch all for things to do if the first condition isnt met. For example:
|
You can make it more powerful by specifying an else. This is a catch all for things to do if the first condition isnt met. For example:
|
||||||
|
```
|
||||||
if(cute store clerk has brown hair){
|
if(cute store clerk has brown hair){
|
||||||
give phone number
|
give phone number
|
||||||
} else {
|
} else {
|
||||||
leave store
|
leave store
|
||||||
}
|
}
|
||||||
|
```
|
||||||
Finally, we can specify multiple outcomes using an else if.
|
Finally, we can specify multiple outcomes using an else if.
|
||||||
|
```
|
||||||
if(cute clerk has brown hair){
|
if(cute clerk has brown hair){
|
||||||
give phone number
|
give phone number
|
||||||
} elseif(cute clerk has blond hair) {
|
} elseif(cute clerk has blond hair) {
|
||||||
@ -51,12 +52,12 @@ if(cute clerk has brown hair){
|
|||||||
} else {
|
} else {
|
||||||
buy playstation
|
buy playstation
|
||||||
}
|
}
|
||||||
|
```
|
||||||
Instead of this plain english example, also known as [Pseudocode](http://en.wikipedia.org/wiki/Pseudocode)
|
Instead of this plain english example, also known as [Pseudocode](http://en.wikipedia.org/wiki/Pseudocode)
|
||||||
|
|
||||||
Get the idea?
|
Get the idea?
|
||||||
|
|
||||||
###Fizzbuzz
|
#### Fizzbuzz
|
||||||
|
|
||||||
Ok, now onto FizzBuzz.
|
Ok, now onto FizzBuzz.
|
||||||
|
|
||||||
@ -64,10 +65,10 @@ Open the main.js file and find the loop inside the document ready.
|
|||||||
|
|
||||||
Write code that does the following:
|
Write code that does the following:
|
||||||
|
|
||||||
if i is divisable by 3, append a line that reads "Fizz"
|
* if i is divisable by 3, append a line that reads "Fizz"
|
||||||
|
|
||||||
if i is divisable by 5, append a line that reads "Buzz"
|
* if i is divisable by 5, append a line that reads "Buzz"
|
||||||
|
|
||||||
if i is divisable by 3 & 5, append a line that reads "FizzBuzz"
|
* if i is divisable by 3 & 5, append a line that reads "FizzBuzz"
|
||||||
|
|
||||||
All this should be done as usual, to the printout div.
|
All this should be done as usual, to the printout div.
|
Loading…
x
Reference in New Issue
Block a user