Im trying to solve this kata:
https://www.codewars.com/kata/63f96036b15a210058300ca9/train/javascript
"In this kata, you need to write a function that takes a string and a letter as input and then returns the index of the second occurrence of that letter in the string. If there is no such letter in the string, then the function should return -1. If the letter occurs only once in the string, then -1 should also be returned."
Im useing ASCII code to to convert letters in order to distinguish capital and small letters. But i get problem with random tests. I have no idea what im doing wrong…
- Im converting array and the symbol into ASCII code.
- Im using if statement: If the symbol isn’t in the array I return -1.
- If the symbol first and last index don’t match I convert the first one into ‘=’ and return the second occurrence.
- In the rest of the cases I return the first and only occurrence.
const secondSymbol = (str, symbol) => {
console.log(str, symbol);
let symbolAscii = symbol.charCodeAt(0);
const strArrAscii = str.split('').map(ele => ele.charCodeAt());
if (strArrAscii.indexOf(symbolAscii) === -1) return -1;
else if ((strArrAscii.indexOf(symbolAscii) !== strArrAscii.lastIndexOf(symbolAscii)))
{
strArrAscii[strArrAscii.indexOf(symbolAscii)] = '=';
return strArrAscii.indexOf(symbolAscii);
}
else return strArrAscii.indexOf(symbolAscii);
}
secondSymbol('CQigLHAAxTuCezwqbqMeGOnECbSyLdNfYUxqkLAUvPZzSFbhTfwSVGWzuqvotdOjRxksLRIAQQvogvUmYAr p
fYSbpOOLcxuzD', 'L');
6
Answers
OMG... i didn't read the whole Instruction: "If the letter occurs only once in the string, then -1 should also be returned." I also learned that indexOf() distinguishes capital and small letters... I guess the lesson is don't do katas when You have high fewer because You will make silly mistakes xD
Shouldn’t the last else return -1?
Your method is quite painful. Why not try this approach:
Extract the first character. No need to convert the characters into numbers.
Look for the index of that character in str.slice(1). This will look in the remainder of
str
.Obviously you must add 1 to the result if it is not -1.
I think you are overcomplicating things a bit.
Compare with this, working solution:
Explanation:
The problem is in your final
else
block. This deals with the case when there is only one occurrence of the letter. The challenge explains that you should return -1 in that case, yet you return the index of that character.So change that final
else
from this:to this:
Remarks
There is no need to convert characters to their numerical code. In JavaScript you can compare characters without any need of conversion.
There is no need to create an array from your string (using
split("")
). You can get the index of a character by directly callingindexOf
on the string.You can make use of the second argument of the
indexOf
method, which is useful to find the second occurrence.Like this:
OK, so here’s my code:
Works well, and is short. 🙂
Short one: Use matchAll that will automatically convert the string char into a regex with a ‘g’ flag (so we don’t need to do it), then we spread the iterator returned from matchAll into an array. Each item is an object that (among other things) contains the index of where the match was found. Return the index of the second item, or if no second item, return -1:
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll