0

I'm sorry that this issue is hard to explain in a simple question but I have encountered it a couple times now and do not understand the functionality.

function checkArrays(batch){
  let newArray = [];
  for(var i = 0; i < batch.length; i++){
    let firstDigit = batch[i][0];
    if(newArray.indexOf(firstDigit) === -1){
      newArray.push(firstDigit);
    }
  }
  return new
}

corrected variable from new to newArray

This function takes in batch, a collection of arrays of single digits like [5,7,7,3,9,1,6,3]. The goal is to take the first digit of each array and add them to new, which is returned. My question is why is it necessary to check if new.indexOf(firstDigit) === -1 in order to do this? What is significant about -1 and why would one want to iterate this way? Thanks!

6
  • 1
    new is a reserved keyword and cannot be used as a variable name. indexOf returns -1 since there's no way to have an element at position -1 in an array. Commented Nov 19, 2020 at 19:51
  • 1
    Hint: take a look at MDN's documentation for indexof: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 19, 2020 at 19:51
  • Because you tried to create a variable called new, and new is a reserved keyword, this is not a valid JavaScript program. Can you please change the question and example to use a different name for that variable so we can move past that part of the issue? Commented Nov 19, 2020 at 20:12
  • The logic here is "if it's not in the array yet, then add it". But this is sounding a lot like what a Set is for. Commented Nov 19, 2020 at 20:14
  • or use reduce playcode.io/706397 Commented Nov 19, 2020 at 20:21

1 Answer 1

3

This:

if(new.indexOf(firstDigit) === -1)

Basically means "if the element is not in the array", because indexOf:

... returns the first index at which a given element can be found in the array, or -1 if it is not present.

In most browsers you can also use includes which is more semantically descriptive:

if(!new.includes(firstDigit))

As an aside... new is probably not a great name for a variable, since it's an operator in the language itself. For this particular operation I'd go with something like result or newArray or something else a little more descriptive and not reserved.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. This function will only return the first digit of arrays if they are unique, why is this?
@IanWiseman: Because that's what the function is written to do. Any reasoning behind that is a question for whomever wrote it or decides that business functionality.
Sorry, my question was not about why it does this, but rather how does it do it. I see now that it's checking to see if a first digit is not in new and then pushing that given digit if the answer is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.