4. Arrays

Que.1 Find the Product.

            
  const Find_Prod = (array, N) => 
  {
     let Prod=1; // declare Prod and initialize with 1
     
     for(let i=0; i<N; i++){
       Prod *=array[i]; // Prod= 1x1x2x3x4x5= 120
     }
     return Prod; // 120
  };

        

Que.2 Find the Sum.

            
  const Find_Sum = (array, N) => 
  {
    let sum = 0;
    for(let i=0; i<N; i++){
      sum = sum + array[i];
    }
    return sum;
  
  };
        

Que.3 Count Occurrences

  const findCount = (N, K, Arr) => 
  {
    let count = 0;
      for(let i=0; i<N; i++){
        if(Arr[i]===K){ 
          // if array element value and data type is equal to k then increase count value
          count++;
        }
        
      }
      return count; // return final result of count
  };
        

Que.4 Even Odd

            
  const findEvenOdd = (N, Arr) => 
  {
      let Odd = 0; // Declare and initilize with 0 of Odd
      let even = 0; // Declare and initilize with 0 of even
      for(let i=0; i<N; i++){ // Now check every array element
        if(Arr[i]%2===0){  // for even element
          even += Arr[i];
        }
        else{
          Odd += Arr[i]; // for odd element
        }
      }
       return [even, Odd]; // final result sum of odd number's and even number's
  };          

        

Que.5 Find whether the number is present or not

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

Que.6 Higher Age

            
  const highAge = (N, Arr) => 
  {
      const answer =[]; // other array
      for(let i in Arr){
        if(Arr[i]>=18){
          answer.push(Arr[i]);
        }
      }
      return answer;
  }; 
        

Que.7 Increment the Array Elements

  const Inc_Arr = (array,N) => 
  {
    const Ans = array.map(x => x + 1); // map is a built-in method of an array
    // x is current array and  x + 1 is increase 1 of array's per value
    console.log(Ans.join(" "));
  }; 
        

Que.8 Pass or Fail

 
  const isAllPass = (N, Arr) => 
  {
    for(let i=0; i < N; i++){
      for(let j=i+1; j < N; j++){
        if(Arr[i]>=32 && Arr[j]>=32){
          return "YES";
        }
        else{
          return "NO";
        }
      }
    } 
    return "No";
  };        
        

Que.9 Unique Color Shirt

  function Unique_Shirts (arr,N) {
   const num = []; 
   let Count = 0; // count of unique colors
               
   for (let i = 0; i < N; i++) {
     const color = arr[i];
     num[color] = (num[color] || 0) + 1;
   }
    // count the number of unique colors
   for (let color in num) {
     if (num[color] === 1) {
       Count++;
     }
   }
  
    return Count;
  }
        

Que.10 Min and Max

            
  function arrayMin(arr) {
    let minVal = arr[0];
    for (let i = 1; i < arr.length; i++) {
      if (arr[i] < minVal) {
        minVal = arr[i];
      }
      
    }
    
    return minVal;
  }
  
  function arrayMax(arr) {
   let maxVal = arr[0];
   for (let i = 1; i < arr.length; i++) {
      if (arr[i] > maxVal) {
        maxVal = arr[i];
      }
    }
    return maxVal;
   
  }
        

Que.11 Birthday Game

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