-1

The function should take an array, and compare the first item in an array with others. Eventually, it should print on console "True" if all the items an an array the same, and "False" otherwise. My function does the following:

function isUniform(list) {
    var firstItem = list[0];
    for (var i = 0; i <=list.length; i++){
        i++
        if(firstItem === list[i]){
            console.log("True");
        }else {
            console.log("False");
        };
    };
}


input:
isUniform(['a','a','a'])
**********************
output:
VM512:6 True
VM512:8 False 

Could you please give me some insight on what I should change?

2 Answers2

1

The correct algorithm for what you try to achieve is:

function isUniform (list) {
    var firstItem = list[0];
    for (var i = 0; i < list.length; i++){
        if (firstItem === list[i]) {
            console.log("True");
        } else {
            console.log("False");
        };
    };
}

isUniform(['a','a','a'])

You don't need to increment your i in your for loop, the loop already does that. And you need to iterate until i < list.length, not i <= list.length, that is why you got a false log at the end, you are out of your array. So the condition is firstItem === undefined.

https://jsfiddle.net/9L20engu/

Zooly
  • 4,736
  • 4
  • 34
  • 54
0

You could use Array.every https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every, to make sure every item is the same as the first..

But a more optimal version would be Array.some, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some This is because it can then early terminate the loop. The logic is slightly reversed, with having to do a double inequality check.

I've also included using a for loop, more akin to OP's knowledge.

eg.

//use every, this is saying make sure every item is the same as first
//but does mean every item needs traversing
function isUniform(list) {
  const firstItem = list[0];;
  return list.every(i => i === firstItem);
}

//using some we could early terminate the loop by stopping at the first
//item that is not equal,.  and then NOT the result.
function isUniform2(list) {
  const firstItem = list[0];;
  return !list.some(i => i !== firstItem);
}

//using a good old for loop
//this is basically working like the Array.some, basically compare each item
//to the first, once we get a difference return false, if we make it all the
//way through the array, it must mean there all equal.
function isUniform3(list) {
  var firstItem = list[0];
  for (var i = 0; i < list.length; i++){
    if (firstItem !== list[i]) return false;
  }
  return true;
}



console.log('using every');
console.log(isUniform(['a','a','a']));
console.log(isUniform(['a','b','a']));
console.log('using some');
console.log(isUniform2(['a','a','a']));
console.log(isUniform2(['a','b','a']));
console.log('for loop');
console.log(isUniform3(['a','a','a']));
console.log(isUniform3(['a','b','a']));
Keith
  • 22,005
  • 2
  • 27
  • 44