How to return an item at a particular index

Tell us what’s happening:
i am trying to loop the items in an array starting from the last item in that particular array. but .length-1 gives me the index and not the item at that index

Your code so far


function confirmEnding(str, target) {
 let newArr = [];
 for(let split of str){
 //console.log(split)
 newArr.push(split);
 }
// console.log(newArr)
 if(newArr.length-1 === target){
   return true;
 }else{
   return false;
 }
 //return str;
}

confirmEnding("Bastian", "n");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:

if(newArr[newArr.length-1]

You already had the index but you just had some of the syntax wrong. To get the element of an array you put it in this form: Arr[index]! Happy Coding!:smile:

1 Like

target is a string (of one character) and
newArr.length-1 is a number so you can’t compare these two to see if they are equal because they will never be. In the if condition you want to compare the value of the last entry in newArr with the target. There are many ways to get the last value in an array. How would you get the first value in an array?

newArr[newArr.length-1] will give me the last element in the array. it same as newArr[index of last element(newArr.length-1)].

Not saying this is the wrong approach, but it may be that you’re over-thinking this a little. Have you taken a look at String.substring(…) yet? It may be a more straightforward route to what you’re trying to do.

Consider the logic - you want to compare the last <length of comparator> letters of <string> to <comparator> itself. .substring(...) can help you do just that.

See, you got to remember, you might not only have confirmEnding("Bastien", "n"), you might also get confirmEnding("Congrats", "rats") - and your current approach won’t really allow that.

i am currently working on this, it will be handled by suming from behind and comparing it to the target

I’m really curious to see how you’ll get that working via an array like that. I mean, maybe you could convert the comparator to an array as well, and then compare using negative indices on both…

1 Like

Actually, to do this? You don’t even need to convert them to an array. Strings are already “array-like” in javascript, in that you can do something like str[str.length-i], and it will access the string position that way.

Just pointing that out, in case it helps. :wink:

yea, the only reason i coverted to arrray is because i want a situation where by i can be able to access the last element in the array and other item before it

1 Like

i am trying to loop the splited string from the and and compare with the given target. i dont wht the code is not working.

  let newArr = [];
  let word = [];
  word = str.split(" ");
  console.log(word.length)
  if(word.length == 1){
    for(let splits of str){
         //console.log(splits)
          newArr.push(splits);
       for(let i = 1;i<splits.length; i++){
           let newWord;
           newWord = newArr[newArr.length-i] + newWord;
           if(newWord == target){
             //console.log(newWord)
             return true;
           }
           else{
             return false
           }
        }
  }
  }
  else if(word.length>1 && word[word.length-1] == target){
   return true;
  }
  else{
    return false;
  }
  
}

confirmEnding("Bastian", "n");

is that your whole code? where is the function signature?

anyway, you could follow your code with a tool like this: http://pythontutor.com/javascript.html#mode=edit
so you can see when the code stop doing what you think it should be doing