Returning the Index number when a function is called on multiple items

Returning the Index number when a function is called on multiple items
0

#1

I assumed that if I made a function that function (elementValue, index) was the default layout but I think Im confusing this with higherOrder functions

Is there a way I can get the index number of the current Elementvalue that a forEach is being called upon?

I want to return the index number where sheep is peehs
I guess I could do a count though but Is there a better way?

function findWrongWayAnimal (field) {
  
    /*
    This array takes an array of animals in the format:
    ['sheep', 'sheep', 'sheep', 'sheep', 'peehs', 'sheep']
    
    The animals will all be the 'right way round' apart from 1 animal!
    Your function should return its index position. E.g. in this example, 4.
    NB The animals will not always be sheep but it will always be a field of the same kind of animal.
    */
   var wrongWayIndex;
    function wrongWay(Item , Index )
    {
      var itemReverse = Item.split('').reverse().join(' ')
     
      

      if(Item === itemReverse)
      {
        return index
        wrongWayIndex = index

       
      }
    }
    field.forEach(wrongWay);
    console.log(wrongWayIndex)
  
  }

findWrongWayAnimal(['sheep', 'sheep', 'sheep', 'sheep', 'peehs', 'sheep'])

#2

This is probably a better way of doing it

But why is it not returning the x index number when the conditions are met

  
  function findWrongWayAnimal (field)
  {
  
    
  let wrongWayIndex = 0
    

  for(x=0;x<field.length;x++)
 {
   var itemReverse = field[x].split('').reverse().join(' ');

   if(field[x] === itemReverse)
    { 
       
        wrongWayIndex = x
     
    }
   
  }

        
   console.log(wrongWayIndex)
  return wrongWayIndex
}

findWrongWayAnimal(['sheep', 'sheep', 'sheep', 'sheep', 'peehs', 'sheep'])



#3

It always returns 0 index, because you declared let wrongWayIndex = 0. And below, when you test words from array, this condition if(field[x] === itemReverse) is never met because in this particular example ‘sheep’ === ‘peehs’ is false…


#4

If you want to use the array methods, look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex. forEach is the wrong function - what you want to do is find an index, not do an operation on every element.

You have the right idea with your second set of code (bearing in mind what @ace1122sp says, as it currently won’t work) though it is a different way of doing things (imperative, rather than declarative). A loop is probably easier than using a higher-order function, because you need to do a couple of things at once (check what the “right way round” looks like and find the word that’s the reverse of that)

EDIT: also note that the fact that the string you’re looking for is reversed is irrelevant. One string in the array is not the same as the other strings: that’s what’s important


#5

You need to figure out how to find that one word, which is different than all other words… You just need to find first 2 words that are the same and then you will know what is the correct word. From there on, compare all other words with the correct word.


#6

Did it through using two loops



 function findWrongWayAnimal (field)
 {
   var animal;
   
   for(x=0;x<field.length;x++)
   {
     if(field[x]===field[x+1])
     {
       
       var animal = field[x]
       
       
     }
   }
  for(x=0;x<field.length;x++)
  {
    if(field[x]!==animal)
    {
      return x
    }
  }
   
 }