//pass an array of numbers into a function and log each number to the console
function yourFunctionsName(arrayToLoop){
//Initialize 'i' as your counter set to 0
//Keep looping while your counter 'i' is less than your arrays length
//After each loop the counter 'i' is to increased by 1
for(let i = 0; i <arrayToLoop.length; i++){
//during each loop we will console.log the current array's element
//we use 'i' to designate the current element's index in the array
console.log(arrayToLoop[i])
}
}
//Function call below to pass in example array of numbers
yourFunctionsName([1, 2, 3, 4, 5, 6])
//this varible as the name implies is the amount of wanted loops
var amountOfLoops = 3
for (let i=0; i<amountOfLoops; i++) {
//code to run each iteration
}
let arry = {
name: "Rakibul Islam",
class: "Tane",
grup: "A",
roll: 10,
sub: "Arse"
}
//for in loop used to object
for(let i in arry){
console.log(i,arry[i]);
}
/*
for (statement1; statement2; statement3) {
INSERT CODE
statement1 is executed before for loop starts
statement2 is the condition
statement3 is executed after every loop
}
*/
for (i = 0; i < 5; i++) {
console.log(i);
}
// Sample Data:
const days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
// Samle Data which has undefined items:
const days = ["Monday","Tuesday",,"Thursday",,"Saturday","Sunday"]
// This Post might be a good read, to see a few caveats and advantages:
// https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea
//Classic For Loop using 3 expressions iterates all array indexes:
for (let i=0; i<days.length; i++){
console.log("Day: "+days[i])
}
//for..in iterates defined array indexes:
for (let day in days){
console.log("Day: ", days[day]);
}
//for..of iterates all array items:
for (let day of days){
console.log("Day: ", day);
}
//using array methods might be more suitable depending on the scenario:
days.forEach(function(day){
console.log("Day: ", day);
});
//available methods:
days.forEach()
days.map()
days.filter()
days.reduce()
days.every()
days.some() //this one is neat imo
...
JS Loops
for (before loop; condition for loop; execute after loop) {
// what to do during the loop
}
for
The most common way to create a loop in Javascript
while
Sets up conditions under which a loop executes
do while
Similar to the while loop, however, it executes at least once and performs a check at the end to
see if the condition is met to execute again
break
Used to stop and exit the cycle at certain conditions
continue
Skip parts of the cycle if certain conditions are met
let items = ['beef', 'cabbage', 'javascript']; // Defines a list of items
for (let item of items) { // Defines for loop with a variable of 'item'
console.log(item); // Prints the item that is found
}
let arry = {
name: "Rakibul Islam",
class: "Tane",
grup: "A",
roll: 10,
sub: "Arse"
}
//for in loop used to object
for(let i in arry){
console.log(i,arry[i]);
}
for (i in things) {
// If things is an array, i will usually contain the array keys *not advised*
// If things is an object, i will contain the member names
// Either way, access values using: things[i]
}
var car = {"Tesla":"Model 3", "Mercedes":"V-8 Model"};
for(brandName in car){
console.log(brandName);
}
//Tesla
//Mercedes
for(brandName in car){
console.log(car[brandName]);
}
//Model 3
//V-8 Model
let i = 0;
//while checking for a state
while (i < 20){
console.log(`do something while ${i} is less than 20`)
i++;
}
console.log("End of While Loop")
//for iterating a certain number of times
for(j = 0; j < 20; j++){
console.log(`do something for ${j + 1} time(s)`)
}
console.log("End of For loop")
var test = 0;
type 1;
while (test < 10) {
console.log(test)
test = test++
}
/* while (condition) {
our code
} */
return(
0
1
2
3
4
5
6
7
8
9) to the from (instant) * 'in console'
type 2;
for (var n = 0;n < 10; n++) {
console.log(n)
}
//note separate codes in for loops with ";"
/* for (Initial value before starting the loop;loop Condition;value after ending loop) {
our code
} */
return(
0
1
2
3
4
5
6
7
8
9) to the from (instant) * 'in console'
for(let i = 1; i <= 5; i++){
if (i % 2 !== 0) console.log("Number", i);
}
let i = 2;
while (i <= 6){
console.log("Number", i);
i++
}
let j = 1;
do {
console.log("Number", j);
j++;
} while(j <= 5)
const user ={
name:'Shirshak',
age:25,
subscibers:200,
money:'lolno'
}
for(let x in user){
console.log(user[x]) //name,age,subscriber and money(only get key not value)
}
for (i = startValue; i <= endValue; i++) {
// Before the loop: i is set to startValue
// After each iteration of the loop: i++ is executed
// The loop continues as long as i <= endValue is true
}
//First, declare a variable for initial value
const doWhile = 10;
//Second, get do-while and write what to execute inside do bracket.
//Then, write ++/-- according to your condition.
//Lastly, inside while bracket write your condition to apply.
do{
console.log(doWhile)
doWhile--
}while(doWhile >= 0)
//first type
for(let i; i< number; i++){
//do stuff
//you can break
break
}
//2nd type
const colors = ["red","green","blue","primary colors"]
colors.forEach((color) =>{
//do stuff
//But you can't breack out of the loop
})
//3rd type might not be considered as a loop
colors.map((color) =>{//do stuff
//no bracking})
How for-loops work
A for loop has 3 parts
I will go the through the first section, than the third and than the second.
The first part is where we will start, in this case, I want to start at 0.
for(let index = 0;)
than we say every time the loop repeats how much does it add?
In this case Im using numbers and adding 1 each time. so I say:
for(let index = 0; index = index + 1)
And the final part when do we want the loop to stop?
in this case I want it to stop at 10 so I will make my for-loop like this:
for(let index = 0; index < 10; index = index + 1)
Now I add the body to my for-loop
for(let index = 0; index < 10; index = index + 1) {
}
And now inside the body I run the command: console.log(index);
this will run the for-loop
for(let index = 0; index < 10; index = index + 1) {
console.log(index); } //-> 0 1 2 3 4 5 6 7 8 9
It will run to 9 not 10 because it did run 10 times, but
the index started at 0 not 1
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
for (let i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}