Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="Loops">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*
- LOOPS.
- In JavaScript Loops are especially helpful and invaluable in the sense that they allow us
- to run a block of code however many times we'd like. Also Loops allow us to iterate over
- collections of data & pull values from them. Loops check a condition. If that condition
- returns true,a block of code will run. Then the condition will be checked again and if it
- still returns true, the code block will run again. The Loop repeats this process until the
- condition returns false.
- Here are 3 common types of Loops:
- */
- //For Loops:
- for(var j = 1; j <= 9; j++) {
- console.log(j);// Logs 1-9, loop ends when J is = to 9
- };
- for(var y = 3; y > 0; y--) {
- console.log(y);// Logs 3-1, loop ends when y is > but not = to 0
- }
- //For Loops w/Arrays: logging a movie list
- var movieList = [ 'Eternal Sunshine of a Spotless Mind', 'Oldboy',
- 'Hanna', 'Vanilla Sky', "Pan's Labyrinth",
- 'Spirited Away', 'Tron'];
- for(let i = 0; i < movieList.length; i++) {
- console.log(movieList[i]);// Logs movie titles to console
- };
- for(var i = movieList.length; i >= 0; i--) {
- console.log(movieList[i]);// Logs titles in reverse
- };
- //While Loops: logging same movie list in a different way
- var list1 = "The Movie List is:\n";
- var list2 = "";
- var x = 1;
- while( x < movieList.length) {
- list2 += movieList[x]+"\n";
- x++;
- var list3 = list1 + list2;
- };
- console.log(list3);//Logs titles with a line break after each
- //For-In Loops: looping over objects
- var xxSongList = {
- song1: "lips",
- song2: "Infinity",
- song3: "Sunset",
- song4: "Islands",
- song5: "Fiction",
- song6: "Chained",
- song7: "Dangerous"
- };
- for(var q in xxSongList) {
- console.log(xxSongList[q]);
- }
- //For-In Loops: looping over objects backwards
- function reverseObj(obj) {
- var newArr = [];
- for(var key in obj) {
- if(obj.hasOwnProperty(key)) {
- newArr.push(obj[key]);
- }
- }
- for(var v = newArr.length-1; v >=0; v--){
- console.log(newArr[v]);
- }
- }
- reverseObj(xxSongList);// Logs The XX song list in reverse
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*
- LOOPS.
- In JavaScript Loops are especially helpful and invaluable in the sense that they allow us
- to run a block of code however many times we'd like. Also Loops allow us to iterate over
- collections of data & pull values from them. Loops check a condition. If that condition
- returns true,a block of code will run. Then the condition will be checked again and if it
- still returns true, the code block will run again. The Loop repeats this process until the
- condition returns false.
- Here are 3 common types of Loops:
- */
- //For Loops:
- for(var j = 1; j <= 9; j++) {
- console.log(j);// Logs 1-9, loop ends when J is = to 9
- };
- for(var y = 3; y > 0; y--) {
- console.log(y);// Logs 3-1, loop ends when y is > but not = to 0
- }
- //For Loops w/Arrays: logging a movie list
- var movieList = [ 'Eternal Sunshine of a Spotless Mind', 'Oldboy',
- 'Hanna', 'Vanilla Sky', "Pan's Labyrinth",
- 'Spirited Away', 'Tron'];
- for(let i = 0; i < movieList.length; i++) {
- console.log(movieList[i]);// Logs movie titles to console
- };
- for(var i = movieList.length; i >= 0; i--) {
- console.log(movieList[i]);// Logs titles in reverse
- };
- //While Loops: logging same movie list in a different way
- var list1 = "The Movie List is:\n";
- var list2 = "";
- var x = 1;
- while( x < movieList.length) {
- list2 += movieList[x]+"\n";
- x++;
- var list3 = list1 + list2;
- };
- console.log(list3);//Logs titles with a line break after each
- //For-In Loops: looping over objects
- var xxSongList = {
- song1: "lips",
- song2: "Infinity",
- song3: "Sunset",
- song4: "Islands",
- song5: "Fiction",
- song6: "Chained",
- song7: "Dangerous"
- };
- for(var q in xxSongList) {
- console.log(xxSongList[q]);
- }
- //For-In Loops: looping over objects backwards
- function reverseObj(obj) {
- var newArr = [];
- for(var key in obj) {
- if(obj.hasOwnProperty(key)) {
- newArr.push(obj[key]);
- }
- }
- for(var v = newArr.length-1; v >=0; v--){
- console.log(newArr[v]);
- }
- }
- reverseObj(xxSongList);// Logs The XX song list in reverse</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement