JS - Day 7

// for loop

for(let i = 0; i < 4; i++){
    console.log(i) // prints the indexes upto 3 from 0 i.e. 0,1,2,3 using loop
}

for(let i = 1; i < 4; i++){
    console.log(i) // prints the indexes upto 3 from 1 i.e. 1,2,3 using loop
}

for(let i = 0; i < 4; i++){
    console.log(fruits[i]) // prints the elements upto 3 index for array of fruits from 0 i.e. 0,1,2,3
}

for(let i = 0; i < 3; i++){
    console.log(fruits[i]) // prints the elements upto 2 index for array of fruits from 0 i.e. 0,1,2
}


// *program 1 using for loop*

// lets consider we have to calculate ages of some people, we have their year of birth

let years = [1989,1990,1991,1992]
let ages = [] // blank array to print the required value in this blank array

for(let i = 0; i < 4; i++){
    console.log(2022-years[i]) // we will get the ages of elements in years array but not in that blank array

    // now to get the ages in that blank array, we will use 'push' method
    
    let b = 2022 - years[i]
    ages.push(b) // we used push method to push the calculated ages in blank array of ages 
}
console.log(ages) // this will print calculated ages in array of ages

// *program 2 using loop*

// lets consider we have to sort some numbers which are above 30 from a list of numbers

let numbers = [44, 55, 11, 22, 33, 10]
let above30 =[] // blank array to print the required numbers in this array

for(let i = 0; i < numbers.length; i++){
    if(numbers[i] > 30){
        above30.push(numbers[i])
    }
}
console.log(above30)

// *program 3 using loop*

// lets consider we have to do sum of marks of students

let marks = [35, 70, 90]
let sum = 0

for(let i =0; i < marks.length; i++){
    sum = sum + marks[i]
}
console.log(sum)

Comments

Popular posts from this blog

What is IP and TCP - Marathi

JS - Day 1

JS - basic types and ways of writing function