Day-6- Loops in JS
Table of contents
No headings in the article.
//Iterate 0 to 10 using for loop, do the same using while and do while loop
for(let i = 0; i<countries.length; i++){
console.log(i,countries[i])
}
//using while loop
let i = 0
while(i<=countries.length){
console.log("In while loop",i,countries[i])
i++;
}
//using do while loop
let j =0;
do{
console.log("In do while loop",j+1,countries[j])
j++;
}while(j<countries.length)
//Iterate 10 to 0 using for loop, do the same using while and do while loop
for(let i = 10; i>=0; i--){
console.log(i)
}
//using do while loop
let i = 10;
do{
console.log("using do while",i)
i--;
}while(i>=0)
//using while loop
let j = 10;
while(j>=0){
console.log("using while loop",j)
j--;
}
//iterate 0 to n using for loop
var n = parseInt(prompt("Enter the value of n"));
if(!isNaN(n)&&typeof n === 'number'){
for(let i = 0; i<n;i++){
console.log(i)
}
}else{
console.log("Please Enter a nummber")
}
//iterate 0 to n using for loop
var n = prompt("Enter the value of n")
for(let i = 0; i<n;i++){
console.log(i)
}
- Write a loop that makes the following pattern using console.log():
#
##
###
####
#####
######
#######
Solution:
for(let i = 1; i<8;i++){
console.log("#".repeat(i));
}
//using traditional method
for(let i = 1; i<8;i++){
let count = ''
for(let j = 1;j<=i;j++){
count+="#"
console.log("i am in j loop")
}
console.log(count);
}
- Use loop to print the following pattern:
0 x 0 = 0
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81
10 x 10 = 100
Solution
for(let i = 0; i<=10; i++){
console.log(i+" x "+i+" = "+i*i)
}
- Using loop print the following pattern
i i^2 i^3
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
for(let i = 0; i<=10;i++){
console.log(i ,Math.pow(i,2),Math.pow(i,3))
}
Use for loop to iterate from 0 to 100 and print only even numbers
for(let i = 0;i<=100;i++){ if(i%2===0){ console.log(i) } }
Use for loop to iterate from 0 to 100 and print only odd numbers
for(let i = 0;i<=100;i++){ if(i%2!==0){ console.log(i) } }
Use for loop to iterate from 0 to 100 and print only prime numbers
Prime numbers have only 2 factors, one themselves and other is one. We have to find it using JavaScript. so first off, we know that 0 and 1 are not prime numbers.
The loop will check if the number divides with no remainder left, we check until the square of the number.
const isPrime = (num)=>{ if(num<=1) return false; for(let i = 2; i<Math.sqrt(num); i++){ if(num%2===0) return false } return true; } for(let i = 0; i<=100; i++){ if(isPrime(i)){ console.log(i) } }
Use for loop to iterate from 0 to 100 and print the sum of all numbers.
let sum = 0; for(let i =0; i<=100; i++){ sum+=i; } console.log(sum)
Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds.
//output The sum of all evens from 0 to 100 is 2550. And the sum of all odds from 0 to 100 is 2500. //solution let sum = 0; let Oddsum = 0; for(let i =0; i<=100; i++){ if(i%2===0) sum+=i; else Oddsum+=i; } console.log("The sum of all evens from 0 to 100 is", sum + ".And the sum of all odds from 0 to 100 is "+ Oddsum )
Now, Print sum of evens and sum of odds as array.
let arrayNum = []; let sum = 0; let Oddsum = 0; for(let i =0; i<=100; i++){ if(i%2===0) sum+=i; else Oddsum+=i; } arrayNum.push(sum); arrayNum.push(Oddsum) console.log(arrayNum)
Develop a small script which generate array of 5 random numbers
We can get random numbers in Javascript using Math.random
//Develop a small script which generate array of 5 random numbers let random5Numbers = []; for(let i = 0; i<5; i++){ random5Numbers.push(Math.floor(Math.random()*100)) } console.log(random5Numbers);
Develop a small script which generate array of 5 random numbers and the numbers must be unique
let random5UniqueNumbers = []; for(let i = 0; i<5; i++){ let randomNumber = Math.floor(Math.random()*100) if(random5UniqueNumbers.indexOf(randomNumber)===-1) random5UniqueNumbers.push(randomNumber) } console.log(random5UniqueNumbers)
Develop a small script which generate a six characters random id:
const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const generateID =() => { let ID = ''; const charactersLength = characters.length; for(let i = 0; i<6; i++){ ID += characters.charAt(Math.floor(Math.random()*charactersLength)) } return ID; } console.log(generateID())
Exercise-2
Develop a small script which generate any number of characters random id:
const generateID = (length)=>{
const characters = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let ID = '';
const charactersLength = characters.length;
for(let i = 0; i<length; i++){
ID += characters.charAt(Math.floor(Math.random()*charactersLength))
}
return ID;
}
length = Math.floor(Math.random()*20)
console.log(generateID(length))
Write a script which generates a random hexadecimal number.
const generateHexId = () =>{
const hexID = '0123456789ABCDEF';
let randomHex = '';
for(let i = 0; i<6;i++){
randomHex += hexID.charAt(Math.floor(Math.random()*(hexID.length)))
}
return randomHex;
}
console.log(generateHexId())
Write a script which generates a random rgb color number.
const randomRGB = () => {
const red = Math.floor(Math.random()*256);
const green = Math.floor(Math.random()*256);
const blue = Math.floor(Math.random()*256);
return `rgb(${red},${green},${blue})`
}
console.log(randomRGB())