Start with Python
This commit is contained in:
72
2- javascript/4-while-loops-and-for-each/README.md
Normal file
72
2- javascript/4-while-loops-and-for-each/README.md
Normal file
@ -0,0 +1,72 @@
|
||||
While and For In Loops
|
||||
========================
|
||||
|
||||
In this challenge, we're going to write a while loop, a For In loop, and nest them.
|
||||
|
||||
#### While Loops
|
||||
|
||||
A while loop iterates until a condition is no longer true. For example:
|
||||
```
|
||||
while(greg == alive){
|
||||
live_year(1)
|
||||
age++
|
||||
}
|
||||
```
|
||||
Seemingly, a while loop could be endless. In that case, we have to break it. Take the following example:
|
||||
```
|
||||
i = 0
|
||||
|
||||
while(true){
|
||||
i++
|
||||
print("I'm an endless loop")
|
||||
if(i >= 50){
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
This will run exactly 50 times.
|
||||
|
||||
#### For In
|
||||
|
||||
A For In loop iterates through each index in the data structure. In an array, an index is an integer, that starts at 0. Take this example:
|
||||
```
|
||||
var array = ['john', 'bobby', 'homa', 'stevie', 'rob']
|
||||
|
||||
for(var i in array) {
|
||||
console.log(i) // prints 0, 1, 2, 3, 4
|
||||
}
|
||||
```
|
||||
In an object however, the index is a string. Check this out:
|
||||
```
|
||||
var obj = {'john': 'student', 'bobby': 'programmer', 'homa': 'actress', 'stevie': 'gamer'}
|
||||
|
||||
for(var k in obj) {
|
||||
console.log(k) // prints 'john', 'bobby', 'homa', 'stevie'
|
||||
}
|
||||
```
|
||||
#### Sandbox
|
||||
|
||||
Try this on your own in your node console or browser console. Declare some objects, iterate through them, print them out.
|
||||
|
||||
Also check out [.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
|
||||
|
||||
#### Multiplication Tables
|
||||
|
||||
Using any loop / loops of your choosing, write code that prints the multiplication tables up to a number input by the user in a form.
|
||||
|
||||
Create the form that gets the user input yourself above the #printout div, and append the result to the printout div.
|
||||
|
||||
Bonus if you actually use a table.
|
||||
|
||||
Your return should look something like this:
|
||||
```
|
||||
// userinput = 7
|
||||
```
|
||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
||||
1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
||||
2 | 2 | 4 | 6 | 8 | 10| 12| 14|
|
||||
3 | 3 | 6 | 9 | 12| 15| 18| 21|
|
||||
4 | 4 | 8 | 12| 16| 20| 24| 28|
|
||||
5 | 5 | 10| 15| 20| 25| 30| 35|
|
||||
6 | 6 | 12| 18| 24| 30| 36| 42|
|
||||
7 | 7 | 14| 21| 28| 35| 42| 49|
|
18
2- javascript/4-while-loops-and-for-each/index.html
Normal file
18
2- javascript/4-while-loops-and-for-each/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>Multiplication Tables</h1>
|
||||
</nav>
|
||||
<div id = "printout"></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
36
2- javascript/4-while-loops-and-for-each/main.css
Normal file
36
2- javascript/4-while-loops-and-for-each/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;
|
||||
}
|
8
2- javascript/4-while-loops-and-for-each/main.js
Normal file
8
2- javascript/4-while-loops-and-for-each/main.js
Normal file
@ -0,0 +1,8 @@
|
||||
// set your variables below
|
||||
|
||||
// all jquery dom actions can only be called inside document ready. Print your variables
|
||||
// inside document ready.
|
||||
|
||||
$(document).ready(function(){
|
||||
|
||||
})
|
47
2- javascript/4-while-loops-and-for-each/reset.css
Normal file
47
2- javascript/4-while-loops-and-for-each/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