js.l25 code example

Example: js.l25

------------------LOOPing-------------------

//for loop----forword------------------------------
// for (let i = 0; i <= 10; i++) {
//    console.log(`looping in for loop ${i}`); 
// };


//for loop----backword------------------------------
// for (let i = 10; i >= 0; i--) {
//    console.log(`looping in for loop ${i}`); 
// };






//using for loop print all the Array value-------------------------------
// const arr = ['Dulon', 'teacher', 'blue',1963,true];
// for (let i = 0; i < arr.length; i++){
//    console.log(`${arr[i]}`);
// };








// looping array push it another array-------------------------
// const arr = ['Dulon', 'blue',1963,true];
// const newarry = [];

// for (let i = 0; i < arr.length; i++){
//    newarry.push(typeof arr[i]);
// };
//    console.log(newarry);








// simple challenge calculate birthyear-----------------------------------
// const birthyear = [1995, 1996, 1997, 1998];
// const age = [];

// for (let i = 0; i < birthyear.length; i++){
//    // console.log(2037 - birthyear[i]);

//    age.push(2037 - birthyear[i])
// }
//    // console.log(birthyear);
//    console.log(age);








// take birthyear in useing array and calculate age to useing array and push new array-------------------------------
// const birthyear = []
// for (let i = 1990; i <= 2000; i++) {
//    // console.log(i);
//    birthyear.push(i)
// }
// console.log(birthyear);
// const age = [];
// for (let i = 0; i < birthyear.length; i++) {
//    // console.log(birthyear[i]);
//    age.push(2037 - birthyear[i])
// }
// console.log(age);




// Continue And Breake in for loop or others loop------------------------------------
// const arr = ['Dulon', 'Mahadi', 16556, 1562,  'manner', true, [1,2] ];
// for (let i = 0; i < arr.length; i++) {
//    // if(typeof arr[i] !== 'string') continue;        // continue.
//    // if(typeof arr[i] === 'boolean') break;        // breake.
//    console.log(`${arr[i]}--------${typeof arr[i]}`);   
// };






//for lop into another for loop----------------------------
// for (let i = 0; i <= 3; i++) {
//    console.log(`LIST-${i}:`);

//    for (let ind = 0; ind <= 2; ind++) {
//       console.log(`index:${i} arguments:${ind}`);

//    }
// }





// While loop--------------------------------------------
// let i = 1;
// while (i <=10 ) {
//    console.log(`while loop: ${i}`);
//    i++;
// }




// // challenge........importent two operation in this challenge........loop.....................................
const bills = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];

const tips = [];
const totals = [];

function calcTip (bill) {
   return bill >= 50 && bill <= 300 ? 0.15 : 0.2;
};

for (let index = 0; index < bills.length; index++) {
   const tip = calcTip(bills[index]);
   tips.push(tip);
   totals.push(tip + bills[index]);
   
}

console.log(`All Bill: "${bills}"`);
console.log(`Tip Per Bill: "${tips}"`);
console.log(`Totals Bill: "${totals}"`);

// avarage bill calculated in array.---------------------------------------------
const calcAvg = (arr) => {
   let sum = 0;
   for (let i = 0; i < arr.length; i++) {
      sum += arr[i];
   }
   console.log(sum / arr.length);
}

calcAvg(totals);


// totals sum of arrays....----------------------------------------------
const bill = [4, 4, 4];

const calc = (arr) => {
   let sum = 0;
   for (let i = 0; i < arr.length; i++) {
      sum = sum + arr[i];
   }
   console.log(sum);
}

calc(bill);

Tags:

Misc Example