JS-Arrays.

Total Number of Questions: 11
  Find the Product.

Write a program that takes an array as input from the user and find out the product of the elements.

Note: Complete the Find_Prod function. No need to take any input.

Ans:-
const Find_Prod = (array, N) => {
    var product = 1;
    for(let i = 0; i <= N; i++){
        if(typeof array[i] == `number`){
        product = array[i] * product;
        }
    }
    return product;
};

  Find the Sum.

Write a program which takes an array as input from the user and find out the sum of the array elements.

Note: Complete the Find_Sum function. No need to take any input.

Ans:-
const Find_Sum = (array, N) => {
    var sum = 0;
    for (var i = 0; i < array.length; i++) {
        if(typeof array[i] == `number`) {
            sum += array[i];
        }  
    }
    return sum;
};

  Count Occurrences.

You are given an array A containing N integer elements and an integer K, and your task is to return the count of occurrences of K in array A

Input Format: The first line of the input contains two space-separated integers N and K, denoting the number of elements in the array A and the element whose count needs to be determined, respectively. The second line of the input contains N space-separated integers, denoting the elements of the array A .

Note: Complete the findCount function. No need to take any input.

Ans:-
const findCount = (N, K, Arr) => {
    var count = 0;
    for(let i = 0; i<Arr.length; i++){
        if(Arr[i] === K){
        count++
        }
    }
    return count;
};

  Even Odd.

You are given an array A containing N integer elements, and your task is to return an array B (having a size equal to 2), where B[0] contains the sum of all even elements of array A and B[1] has the sum of all odd elements of the array A .

Note: Complete the findEvenOdd function. No need to take any input.

Input Format: The first line of the input contains an integer N, denoting the number of elements in the array A. The second line of the input contains N space-separated integers, denoting the elements of array A .

Ans:-
var sumEven = 0;
var sumOdd = 0;
const B = [];
const findEvenOdd = (N, Arr) => {
    for(let i=0; i<Arr.length; i++){
        var check = Arr[i]%2;
        if((Arr[i]%2)===0){
            sumEven = sumEven + Arr[i];
        }
        else{
            sumOdd = sumOdd + Arr[i];
        }
    }
    const B = [sumEven, sumOdd]
    return B;
};

  Find whether the number is present or not.

Write a program which takes an array as input from the user and a number. Check whether the number is present or not.

Input Format: The first line contains an integer N, denoting the size of the array. The second line contains N space-separated integers, denoting the elements of the array. The third line contains an integer M, denoting the element that needs to be searched.

Note: Complete the Find_Num function. No need to take any input.

Ans:-
const Find_Num = (array,N,M) => {
    for(let i=0; i<array.length; i++){
        if(array[i] === M){
            return "YES";
        }
    }
    return "NO";
};

  Higher Age.

You are given an array A containing the age of persons in your locality, and your task is to find and return an array containing the age of persons that are over 18 (18 included).

Note: Also, in the output array, the age should be in the same order as in the input array. Complete the highAge function. No need to take any input.

Ans:-
const highAge = (N, Arr) => {
    var newArr = []
    for(let i=0; i<Arr.length; i++){
        if(Arr[i]>=18){
            newArr.push(Arr[i])
        }
    }
    return newArr
};

  Increment the Array Elements.

You are provided an array of integers and you have to increment all array elements by 1 and then print whole array.

Note: Complete the Prime_Check function. No need to take any input.

Ans:-
const Inc_Arr = (array,N) => {
    for(let i=0; i<array.length; i++){
        array[i] = array[i] + 1;
        console.log(array[i]);
    }
};

  Pass or Fail.

You are given an array A containing the maths marks of students in your class, and your task is to determine if all the students pass in your class or not. A student is declared pass if his maths marks are greater than or equal to 32. If all the students pass in your class, return "YES" (without quotes); otherwise, return "NO" (without quotes)..

Note: Complete the printNumbers function. No need to take any input.

Ans:-
const isAllPass = (N, Arr) => {
    let flag=true;
    for(let i=0;i< N;i++){
        if(Arr[i]< 32){
            flag=false;
            break;
        }
    }
    if(flag===true){
        return "YES";
    }
    else if(flag===false){
        return "NO";
    }
};

  Unique Color Shirt.

Prepbuddy is very tasteful of clothes. He has N numbers of shirts hanging in the hanger in his wardrobe. Prepbuddy likes to wear different colored clothes. So, whenever he see there are two or more shirts with the same color, he removes all the shirt of that color from his wardrobe. Now, he wants to know how many M unique color shirts are left in the wardrobe. Prepbuddy wants you to find M .

Note: As there are many shades of a color so consider integers as a color name. i.e, color of shirt is 0,1,2, … , N.

Ans:-
function Unique_Shirts (arr,N) {
    var temp=0;
    var dress=0;
    for(let i=0;i< N;i++){
        var compare=0;
        for(let j=1;j< N-1;j++){
            if(arr[i]===arr[j]){
                compare++;
            }
        }
        if(compare==1){
            temp=temp+arr[i];
            dress++;
        } 
    }
    return dress;
}

  Min and Max.

Congratulations on making up to this question. Let us give you a fairly simple array problem to solve. If you know how to iterate through the array, you will easily be able to solve this. The problem statement is simple- given N elements, find the minimum and maximum numbers among those elements.

Note: Complete the Prime_Check function. No need to take any input.

Ans:-
function arrayMin(arr) {
    var min = arr[0];
    for(let i=1; i<arr.length; i++){
        if(min > arr[i]){
            min = arr[i];
        }
    }
    return min;
}
 
function arrayMax(arr) {
    var max = arr[0];
    for(let j=1; j<arr.length; j++){
        if(max < arr[j]){
        max = arr[j];
        }
    }
    return max;
};

  Birthday Game.

Yatharth and Anamika are playing a game. Anamika loves Yatharth very much and she wants to share a chocolate bar with him in which each of the squares consists of an integer represented by A[i]. She decides to share a contiguous segment of the chocolate bar selected such that the length of the segment matches Yatharth's birth month M and the sum of the integers on the squares is equal to his birthday D . You must determine how many ways she can divide the chocolate.

Input Format:

  • The first line consists of an integer N which denotes the number of squares in the chocolate bar.
  • The second line consists of N space-separated integers A[i], consisting of numbers on the chocolate bar squares.
  • The third line contains an integer D and M which denotes Yatharth's birthday and his birth month respectively.

Ans:-
function Birthday_Game(arr,D ,M) {
    var count = 0;
    for(let i=0; i< arr.length-M+1; i++){
        var sum = 0;
        for(let j=i; j< M+i; j++){
            sum = arr[j] + sum;
        }
        if(sum===D){
            count++;
        }
    }
    return count;
}