Skip to content

Iterations

// for
for(let i = 0; i <= 10; i++){
const element = i;
if(element == 5){
console.log("5 is the best number");
}
console.log(element);
}
for(let i = 1; i <= 10; i++){
console.log(`Outer loop is value: ${i}`);
for(let j = 1; j <= 10; j++){
// console.log(`Inner value ${j} and inner loop ${i}`);
console.log(i + "*" + j + ' = ' + i*j);
}
}
let myArray = ['flash', 'batman', 'superman'];
for(let index = 0; index < myArray.length; index++){
const element = myArray[index];
console.log(element);
}
for(let index = 1; index <= 20; index++){
if(index == 5){
console.log(`Detected 5`);
break;
}
console.log(`Value of i is ${index}`);
}
for(let index = 1; index <= 20; index++){
if(index == 5){
console.log(`Detected 5`);
continue;
}
console.log(`Value of i is ${index}`);
}
// while, do-while
let index = 0;
while(index <= 10){
console.log(`Value of index is ${index}`);
index = index + 2;
}
let myArray = ['flash', 'batman', 'superman'];
let arr = 0;
while(arr < myArray.length){
console.log(`Value of array is ${myArray[arr]}`);
arr = arr + 1;
}
let score = 11;
do {
console.log(`Score is ${score}`);
score++;
} while (score <= 10);
const arr = [1, 2, 3, 4, 5];
for(const num of arr){
console.log(num);
}
const greetings = "Hello world!";
for(const greet of greetings){
console.log(`Each char is ${greet}`);
}
const map = new Map();
map.set('name', 'John');
map.set('age', 30);
map.set('gender', 'male')
// console.log(map);
for(const [key, value] of map){
console.log(`${key}: ${value}`);
}
const myObject = {
game1: "NFS",
game2: "Spiderman",
}
// for(const [key, value] of myObject){
// console.log(key, ":", value);
// }
const myObject = {
js: "javascript",
cpp: "C++",
rb: "ruby",
swift: "swift by apple",
}
for(const key in myObject){
console.log(`${key} shortcut is for ${myObject[key]}`)
}
const programming = ["js", "rb", "py", "java", "cpp"];
for(const key in programming){
console.log(programming[key]);
}
const map = new Map();
map.set("IN", "India");
map.set("USA", "Unites states of America");
map.set("Fr", "France");
// map.set("In", "India");
for(const key in map){
console.log(key);
}
const coding = ["Js", "ruby", "Java", "pyhton", "cpp"];
coding.forEach(function(val) {
console.log(val);
});
coding.forEach((item) => {
console.log(item);
});
function printMe(item){
console.log(item);
}
coding.forEach(printMe);
coding.forEach((item, index, arr) => {
console.log(item, index, arr);
});
const myCoding = [
{
languageName: "Javascript",
languageFileName: "js"
},
{
languageName: "Java",
languageFileName: "java"
},
{
languageName: "Python",
languageFileName: "py"
},
];
myCoding.forEach((item) => {
console.log(item.languageFileName);
})
const myNums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const newNums = myNums.filter((num) => {
return num > 4;
});
console.log(newNums);
const newNums1 = [];
myNums.forEach((num) => {
if(num > 4){
newNums1.push(num);
}
})
console.log(newNums1);
const books = [
{
title: "Book One", genre: "Fiction", publish: 1981, edition: 2004
},
{
title: "Book Two", genre: "Non-Fiction", publish: 1992, edition: 2008
},
{
title: "Book Three", genre: "History", publish: 1999, edition: 2007
},
{
title: "Book Four", genre: "Non-Fiction", publish: 1989, edition: 2010
},
{
title: "Book Five", genre: "Science", publish: 2009, edition: 2014
},
{
title: "Book Six", genre: "Fiction", publish: 1987, edition: 2010
},
{
title: "Book Seven", genre: "History", publish: 1986, edition: 1996
},
{
title: "Book Eight", genre: "Science", publish: 2011, edition: 2016
},
{
title: "Book Nine", genre: "Non-Fiction", publish: 1981, edition: 1989
},
];
let userBooks = books.filter((bk) => bk.genre === "History");
userBooks = books.filter((bk) => {
return bk.publish >= 1995 && bk.genre === "History"
})
console.log(userBooks);
const myNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// const newNums = myNumbers.map((num) => {return num + 10});
const myNums = myNumbers.map((num) => num * 10)
.map((num) => num + 1)
.filter((num) => num >= 40);
console.log(myNums);