Start with Python
This commit is contained in:
71
2- javascript/3-conditionals-and-for-loops/README.md
Normal file
71
2- javascript/3-conditionals-and-for-loops/README.md
Normal file
@ -0,0 +1,71 @@
|
||||
Conditionals and For Loops
|
||||
==========================
|
||||
|
||||
Get ready for FizzBuzz!
|
||||
|
||||
This is a classic programming test - that yes, you will actually see at job interviews.
|
||||
|
||||
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
|
||||
|
||||
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) }
|
||||
```
|
||||
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 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.
|
||||
```
|
||||
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?
|
||||
|
||||
#### Conditionals - If / Else
|
||||
|
||||
The most simple conditional statement is the if statement. This is also ubiquitous across many programming languages.
|
||||
|
||||
Basically it is written like this:
|
||||
```
|
||||
if(some condition){
|
||||
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:
|
||||
```
|
||||
if(cute store clerk has brown hair){
|
||||
give phone number
|
||||
} else {
|
||||
leave store
|
||||
}
|
||||
```
|
||||
Finally, we can specify multiple outcomes using an else if.
|
||||
```
|
||||
if(cute clerk has brown hair){
|
||||
give phone number
|
||||
} elseif(cute clerk has blond hair) {
|
||||
get phone number
|
||||
} else {
|
||||
buy playstation
|
||||
}
|
||||
```
|
||||
Instead of this plain english example, also known as [Pseudocode](http://en.wikipedia.org/wiki/Pseudocode)
|
||||
|
||||
Get the idea?
|
||||
|
||||
#### Fizzbuzz
|
||||
|
||||
Ok, now onto FizzBuzz.
|
||||
|
||||
Open the main.js file and find the loop inside the document ready.
|
||||
|
||||
Write code that does the following:
|
||||
* if i is divisible by 3, append a line that reads "Fizz"
|
||||
* if i is divisible by 5, append a line that reads "Buzz"
|
||||
* if i is divisible by 3 & 5, append a line that reads "FizzBuzz"
|
||||
|
||||
All this should be done as usual, to the printout div.
|
18
2- javascript/3-conditionals-and-for-loops/index.html
Normal file
18
2- javascript/3-conditionals-and-for-loops/index.html
Normal file
@ -0,0 +1,18 @@
|
||||
<!doctype HTML>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" type="text/css" href="reset.css">
|
||||
<link rel="stylesheet" type="text/css" href="main.css">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="main.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class = "container">
|
||||
<nav id="top">
|
||||
<h1>FizzBuzz</h1>
|
||||
</nav>
|
||||
<div id = "printout"></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
36
2- javascript/3-conditionals-and-for-loops/main.css
Normal file
36
2- javascript/3-conditionals-and-for-loops/main.css
Normal file
@ -0,0 +1,36 @@
|
||||
body{
|
||||
background-color: #D1D1D1
|
||||
}
|
||||
|
||||
nav{
|
||||
position: absolute;
|
||||
background-color: black;
|
||||
width: 100%;
|
||||
height: 4em;
|
||||
top: 0;
|
||||
text-align: center;
|
||||
line-height: 4em;
|
||||
}
|
||||
|
||||
nav h1{
|
||||
font-size: 3em;
|
||||
font-family: monospace;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#printout{
|
||||
width: 800px;
|
||||
min-height: 1000px;
|
||||
background-color: white;
|
||||
margin: 5em auto 0 auto;
|
||||
color: black;
|
||||
text-indent: 1em;
|
||||
}
|
||||
|
||||
#printout p{
|
||||
font-size: 2em;
|
||||
}
|
||||
|
||||
#printout p:nth-child(even) {
|
||||
background-color: #D1D1D1;
|
||||
}
|
12
2- javascript/3-conditionals-and-for-loops/main.js
Normal file
12
2- javascript/3-conditionals-and-for-loops/main.js
Normal file
@ -0,0 +1,12 @@
|
||||
// set your variables below
|
||||
|
||||
// all jquery dom actions can only be called inside document ready. Print your variables
|
||||
// inside document ready.
|
||||
|
||||
$(document).ready(function(){
|
||||
|
||||
for(var i = 0; i<=100; i++){
|
||||
|
||||
}
|
||||
|
||||
})
|
47
2- javascript/3-conditionals-and-for-loops/reset.css
Normal file
47
2- javascript/3-conditionals-and-for-loops/reset.css
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
|
||||
* http://cssreset.com
|
||||
*/
|
||||
html, body, div, span, applet, object, iframe,
|
||||
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
|
||||
a, abbr, acronym, address, big, cite, code,
|
||||
del, dfn, em, img, ins, kbd, q, s, samp,
|
||||
small, strike, strong, sub, sup, tt, var,
|
||||
b, u, i, center,
|
||||
dl, dt, dd, ol, ul, li,
|
||||
fieldset, form, label, legend,
|
||||
table, caption, tbody, tfoot, thead, tr, th, td,
|
||||
article, aside, canvas, details, embed,
|
||||
figure, figcaption, footer, header, hgroup,
|
||||
menu, nav, output, ruby, section, summary,
|
||||
time, mark, audio, video {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
font-size: 100%;
|
||||
font: inherit;
|
||||
vertical-align: baseline;
|
||||
}
|
||||
/* HTML5 display-role reset for older browsers */
|
||||
article, aside, details, figcaption, figure,
|
||||
footer, header, hgroup, menu, nav, section {
|
||||
display: block;
|
||||
}
|
||||
body {
|
||||
line-height: 1;
|
||||
}
|
||||
ol, ul {
|
||||
list-style: none;
|
||||
}
|
||||
blockquote, q {
|
||||
quotes: none;
|
||||
}
|
||||
blockquote:before, blockquote:after,
|
||||
q:before, q:after {
|
||||
content: '';
|
||||
content: none;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
Reference in New Issue
Block a user