Returning the first unique value in a string, ignoring case

So this is a CodeWars project I’m working on, but this is a method of doing something that I have yet to figure out.
I can usually work out how to alter an array to see what I want, I just can never get my programme to return the original value (if that makes sense).

This code should find the first unique value in a string, regardless of case. I want it to return ‘E’. It’s supposed to return all unique values, and then I will get it to return array[0] later.

function firstNonRepeatingLetter(s) {
  // Add your code here
  const result = [];
  
  const word = s.split('');
  
    
  return word.map((item) => {
    const lower = item.toLowerCase();
      if (word.indexOf(lower) === word.lastIndexOf(lower)) {
        return item;
      }
      }
      
  )
    
    
}

firstNonRepeatingLetter('strtrESSd')

I need to make it all lower case in order to check which are unique, and then I want to return the original value. However, it’s given me back 3 s’s at the moment and I don’t know why.

This is something I have tried to do with other arrays, for example an array like [12, 34, 78, 95] and then if I wanted to return the numbers whose digits add up to an even number, I can work that out but that programme returned the digits added up, not the original values in the array.

Please help, I don’t even know how to describe what it is I want to do on Google in order to get a result!

you should convert the whole string to lower case, I am guessing.
Right now, you are only converting the current letter being checked. So it matches the string’s only lower case “s” to your current letters “s”, “S” and “S”. Also, it doesn’t match the string capital “E” with your lowercase test “e”.

This console.log may help:

function firstNonRepeatingLetter(s) {
  // Add your code here
  const result = [];
  const word = s.split('');
    
  return word.map((item) => {
    const lower = item.toLowerCase();
    console.log(lower, item, word.indexOf(lower), word.lastIndexOf(lower));
    if (word.indexOf(lower) === word.lastIndexOf(lower)) {
      return item;
    }
    }   
  )};
}

Hi, thanks
I tried doing that at first outside of the map function, but I couldn’t work out how to get it to return the original.

It’s not just this problem I’m stuck on, I’m not sure how to get anything to return the original value after I’ve iterated through an array to find the values that match what I want.

I need these all to be the same case so I can check, but I don’t know how to return the unique item in the case it originally went into the input…

This might explain it better, it’s what I tried initially:

function firstNonRepeatingLetter(s) {
  // Add your code here
  let result = []

  const word = s.toLowerCase().split('');
  
  return word.map((item) => {
    if (word.indexOf(item) === word.lastIndexOf(item)) {
      return item;
      };
  })   
 
  return word;    
  
    
    
}

firstNonRepeatingLetter('strtrESSd')

So ignoring the undefined in the array which I will deal with later, this returns ‘e’ and ‘d’ as my unique values… how do I get it to return them in the original case ‘E’ and ‘d’?

There are many ways I guess, this quick fix works:

function firstNonRepeatingLetter(s) {
  // Add your code here
  let result = []
  const word = s.toLowerCase().split('');
  return word.map((item, ind) => {
    if (word.indexOf(item) === word.lastIndexOf(item)) {
      return s[ind];
    }
  });
}

I added another variable, ind, into the map method. Then used the index to retrieve the letter from the original string.

Thanks!
But it seems I can’t use this method with filter and forEach which I’d like to do also. Filter would probably be what I want to get rid of the undefineds.
Are there any articles about this online?

create empty array arr = []

split and map the array s = s.split('').map( (i) => {i} )

if (arr does not include i)

push i to to arr

includes() - returns true if array contains an item

'e' === 'E' returns false
so you dont have to try to target specific letter cases

I did it!
Thanks for helping you guys and keeping me motivated to keep finding solutions!

I eventually went with this, not the most elegant solution and I would really like to learn more about transforming all the elements of an array to check something but returning the values in their original form, but since this programme just required returning one item this works for now!

function firstNonRepeatingLetter(s) {
  // Add your code here
  if (s === "") {
    return ""
  }
  else {
  const word = s.toLowerCase().split('');
  let num = word.findIndex((item) => {
    if (word.indexOf(item) === word.lastIndexOf(item)) {
      return item;
      }
  });
  if (s[num] === undefined) {
    return ''
  }
  else{
  return s[num];
  }
  }
}