Practice Set on Arrays in JavaScript

 //Practice Set on Arrays

//Question 1 : Create an array of numbers and take input from the user to add numbers to this array:
let arr = [1, 2, 3, 4, 5, 6, 7];
let a = prompt("Enter a Number : ");
a = Number.parseInt(a);
arr.push(a);
console.log(arr);

//Question 2 : Keep adding numbers to the array in question 1 until 0 is added to the array.
let arr2 = [1, 2, 3, 4, 5, 6, 7, 83];
let a2;
do {
    a2 = prompt("Enter a Number : ");
    a2 = Number.parseInt(a2);
    arr2.push(a2);
} while (a2 != 0);
console.log(arr2);

//Question 3 : Filter for numbers divisible by 10 from a given array.
let arr3 = [1, 2, 30, 4, 50, 6, 7, 83, 670];
let n = arr3.filter((value) => {
    return value % 10 == 0;
});
console.log(n);

//Question 4 : Create an array of square of given numbers.
let arr4 = [1, 2, 30, 4, 50, 6, 7, 83, 670];
let n4 = arr4.map((value) => {
    return value * value;
});
console.log(n4);

//Question 5 : Use reduce to calculate factorial of a given number from an array of first n natural
//numbers (n being the number whose factorial needs to be calculated)
let arr5 = [1, 2, 3, 4, 5];
let n5 = arr5.reduce((x1, x2) => {
    return x1 * x2;
});
console.log(n5);

Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript