'Hello, in this code I need to find exact place of letters, my code finds only place of one letter, but I want to find the place of 2 or more letters
const f = (newStr, givenWord) => {
let arr = [...newStr];
let newArr = [];
for (let i = 0; i + Math.sqrt(arr.length) < arr.length + 1; i += Math.sqrt(arr.length)) {
newArr.push(arr.slice(i, i + Math.sqrt(arr.length)))
}
console.log(newArr);
for (let i = 0; i < newArr.length; i++) {
for (let j = 0; j < newArr[i].length; j++) {
if (newArr[i][j] == givenWord) {
return [i, j];
}
}
}
return [-1, -1];
};
let result = f("QWEASDZXC", "A");
console.log(`[${result[0]} ${result[1]}]`);
Solution 1:[1]
Try with this approach
(function(word, letters) {
// Convert letters words in an array with indexes of each letter in the word
const indexes = letters.split('').map(letter => word.indexOf(letter));
return indexes.filter(index => index !== -1);
})('QWEASDZXC', 'AC');
Input
'QWEASDZXC'
'AC'
Output received
[ 3, 8 ]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Santiago Betancur |
