skip to Main Content

A Meera array is defined to be an array containing only numbers as its elements and for
all n values in the array, the value n*2 is not in the array

So [3, 5, -2] is a Meera array
because 3*2, 5*2 or 2*2 are not in the array. But [8, 3, 4] is not a Meera array because
2*4=8 and both 4 and 8 are elements found in the array.

Write a function that takes an
array of numbered elements and prints “I am a Meera array” in the console if its array
does NOT contain n and also n*2 as value. Otherwise, the function prints “I am NOT a
Meera array”

Test 1: checkMeera([10, 4, 0, 5]) outputs “I am NOT a Meera array” because 5 *
2 is 10
Test 2: checkMeera([7, 4, 9]) outputs “I am a Meera array”
Test 1: checkMeera([1, -6, 4, -3]) outputs “I am NOT a Meera array” because -3
*2 is -6

who to do get the answer

2

Answers


  1. function checkMeera(arr, n) {
      for (let i = 0; i < arr.length; i++) {
        if (arr.includes(arr[i] * 2)) {
          console.log(`[${arr}] is NOT a Meera array because ${arr[i]} * 2 is ${arr[i] * 2}`);
          return;
        }
      }
      console.log(`[${arr}] is a Meera array`);
    }
    
    // Test cases
    checkMeera([10, 4, 0, 5], 5);
    checkMeera([7, 4, 9], 7);
    checkMeera([1, -6, 4, -3], -3); 
    
    Login or Signup to reply.
  2. You can try this function

    const checkMeera = arr => {
      let newArr = arr.map(it => it * 2)
      let word = "I'am Meera Array"
      //check new array for the initial number
      arr.forEach(item => {
        if(newArr.includes(item)){
          word = "I'm not Meera Array"
        }
      })
      return word
    }
    

    and then you can console the value with this test

    let sample2 = [2,3,10,4,8] // Try edit me
    let sample1 = [3,9,8,2] // Try edit me
    // Log to console
    console.log(checkMeera(sample1)) /* return I'm Meera Array */
    console.log(checkMeera(sample2)) /* return I'm not Meera Array */
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search