0

I am doing some challenges on edabit and I came across a problem with my output. So i have to Create a function that takes in an array (slot machine outcome) and returns true if all elements in the array are identical, and false otherwise. The array will contain 4 elements. Can someone explain why my code isn't working the right way.

I have tried with other inputs and it's working fine.

   'use strict';

   function array_Validator(e1,e2,e3,e4)
   {
       let m_Array=[e1,e2,e3,e4];
       for(let i=0;i<m_Array.length-1;i++)
       {
           for(let x=i;x<m_Array.length-1;x++)
           {
               if(m_Array[i]!==m_Array[x+1])
               {
                   return false;
               }
               else
               return true;
           }
       }
   }


  let u_Result=array_Validator("SS","SS","Ss","Ss");

   console.log(u_Result);

So when I input Ss it shoes true instead of false.

Emma
  • 27,428
  • 11
  • 44
  • 69
Johnny Depp
  • 161
  • 1
  • 6
  • .every() would make more sense and not sure why you check things multiple times. – epascarello May 30 '19 at 20:43
  • Note the first iteration of **inner loop**. You have a **return** there. So the function will always finish on the first iteration of the inner loop. Why `true` for your input data? Because for `x = i = 0` the expression `m_array[i] !== m_Array[x+1]` evaluates to `m_array[0] !== m_Array[1]` that results in `"SS" !== "SS"`. That last expression is `false` so `else` is taken and a `true` is returned. – Shidersz May 30 '19 at 20:43
  • Thnx !! I got it. – Johnny Depp May 30 '19 at 20:46
  • `const array_Validator = (...vals) => vals.every(v => v === vals[0])` – epascarello May 30 '19 at 20:52

4 Answers4

1

You need to move return true to the end of the function and you could use a single loop and check the first element with each others.

'use strict';

function array_Validator(e1, e2, e3, e4) {
    let m_Array = [e1, e2, e3, e4];
    for (let i = 1; i < m_Array.length; i++) {
        if (m_Array[0] !== m_Array[i]) return false;
    }
    return true;
}


let u_Result = array_Validator("SS", "SS", "SS", "Ss");

console.log(u_Result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

For this you can use Rest Parameters ...args

function array_Validator(...args) {
    for (let i = 1; i < args.length; i++) {
        if (args[0] !== args[i]) return false;
    }
    return true;
}
let u_Result = array_Validator("SS","SS","Ss","Ss");
console.log(u_Result);
Stefan Avramovic
  • 1,365
  • 2
  • 11
  • 20
0

Note the first iteration of inner loop: you have a return statement there on the if and the else blocks. So the function will always returns on the first iteration of the inner loop.

Why the result is true for your input data "SS","SS","Ss","Ss"?

Because in the first iteration, for x = i = 0, the expression m_array[i] !== m_Array[x+1] evaluates to m_array[0] !== m_Array[1] and that results in "SS" !== "SS". That last expression is then evaluated to false and the else block is taken returning the true value.

There is no need to use nested loops to check if all the elements on an array are equals. Now, you can use Array.every() for this situation.

Example:

function array_Validator(e1, e2, e3, e4)
{
    let m_Array = [e2, e3, e4];
    return m_Array.every(elem => elem === e1);
}    

console.log(array_Validator("SS","SS","Ss","Ss"));
console.log(array_Validator("SS","SS","SS","Ss"));
console.log(array_Validator("SS","SS","SS","SS"));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Community
  • 1
  • 1
Shidersz
  • 16,846
  • 2
  • 23
  • 48
-1

https://stackoverflow.com/a/24968449/11178909

Answer provided, do not use a double for loop for this method first of all

CB-2000
  • 13
  • 4