字符串中最后一个元音的索引
Index of last vowel in a string
我收到了一个项目,我要在该项目中为字符串中最后一次出现的元音建立索引。不幸的是,我一直收到不一致的结果,从自然不会出现的 -1
到 84
或 50
。
我已经尝试 运行 这是一个循环,但不幸的是,我无法掌握任何替代方法来解决这个问题,也无法完全理解为什么我的结果不一致。
我尝试了以下操作:
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh'
let substr = ('a','e','i','o','u');
const lastVowel = myString.lastIndexOf(substr);
console.log(lastVowel);
我不一定要求任何答案、解释,甚至暗示解决方案,我们将不胜感激。但是,如果要粘贴任何内容,我很乐意对其进行修补以进一步理解。我真的不会拒绝任何形式的帮助。
你能把 substr 变成一个数组并遍历它吗?
['a','e','i','o','u'] 而不是 ('a','e' ,'i','o','u').
"('a','e','i','o','u')" 未找到。搜索每一个并找到最大的数字。
Math.max(...substr.map( 元音 => myString.lastIndexOf(元音)))
有几种方法可以解决这个问题,我选择了一种相当懒惰且效率低下的方法。更好的方法是使用单个循环并检查每个元音。这种方式计算每个元音的最后一个位置,取最大的。
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh';
let substr = ['a','e','i','o','u'];
// calculate lastIndex for each vowel separately, then take the greatest
const lastVowel = Math.max.apply(null, substr.map(letter => myString.lastIndexOf(letter)));
console.log(lastVowel, myString[lastVowel]);
由于没有 findLastIndex()
方法 yet,您可以做的是反转数组,获取元音的第一个索引并将其从字符串的 (length-1) 中减去获取您的初始位置:
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh'
let vowels = ['a','e','i','o','u'];
let arr = myString.split("").reverse(); // split string into array, and reverse it
let indexOfLastVowelInReverse = arr.findIndex(e => vowels.includes(e))
if(indexOfLastVowelInReverse != -1) { // if the index is -1 there is no vowel in the string
let index = myString.length-1-indexOfLastVowelInReverse
console.log(`Last vowel found at index ${index}: ${myString[index]}`)
}
我收到了一个项目,我要在该项目中为字符串中最后一次出现的元音建立索引。不幸的是,我一直收到不一致的结果,从自然不会出现的 -1
到 84
或 50
。
我已经尝试 运行 这是一个循环,但不幸的是,我无法掌握任何替代方法来解决这个问题,也无法完全理解为什么我的结果不一致。
我尝试了以下操作:
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh'
let substr = ('a','e','i','o','u');
const lastVowel = myString.lastIndexOf(substr);
console.log(lastVowel);
我不一定要求任何答案、解释,甚至暗示解决方案,我们将不胜感激。但是,如果要粘贴任何内容,我很乐意对其进行修补以进一步理解。我真的不会拒绝任何形式的帮助。
你能把 substr 变成一个数组并遍历它吗?
['a','e','i','o','u'] 而不是 ('a','e' ,'i','o','u').
"('a','e','i','o','u')" 未找到。搜索每一个并找到最大的数字。
Math.max(...substr.map( 元音 => myString.lastIndexOf(元音)))
有几种方法可以解决这个问题,我选择了一种相当懒惰且效率低下的方法。更好的方法是使用单个循环并检查每个元音。这种方式计算每个元音的最后一个位置,取最大的。
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh';
let substr = ['a','e','i','o','u'];
// calculate lastIndex for each vowel separately, then take the greatest
const lastVowel = Math.max.apply(null, substr.map(letter => myString.lastIndexOf(letter)));
console.log(lastVowel, myString[lastVowel]);
由于没有 findLastIndex()
方法 yet,您可以做的是反转数组,获取元音的第一个索引并将其从字符串的 (length-1) 中减去获取您的初始位置:
let myString = 'jrfndklhgfndjkjlkgperfijfhdknsadcvjhiiohjfkledsopiuhgtyujwsdxcvhgfdjhiopiwquhejkdsoiufghedjwsh'
let vowels = ['a','e','i','o','u'];
let arr = myString.split("").reverse(); // split string into array, and reverse it
let indexOfLastVowelInReverse = arr.findIndex(e => vowels.includes(e))
if(indexOfLastVowelInReverse != -1) { // if the index is -1 there is no vowel in the string
let index = myString.length-1-indexOfLastVowelInReverse
console.log(`Last vowel found at index ${index}: ${myString[index]}`)
}