admin管理员组文章数量:1323342
1st week into a JS and trying to solve first Kata in CodeWars.
Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string "This website is for losers LOL!" would bee "Ths wbst s fr lsrs LL!".
My code:
function disemvowel(str) {
var newStr = "";
for (i = 0; i <= str.length; i++) {
if (str.charAt(i) != "a" || str.charAt(i) != "e" || str.charAt(i) != "i" || str.charAt(i) != "o" || str.charAt(i) != "u") {
newStr += str.charAt(i)
}
return newStr;
}
}
1st week into a JS and trying to solve first Kata in CodeWars.
Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string "This website is for losers LOL!" would bee "Ths wbst s fr lsrs LL!".
My code:
function disemvowel(str) {
var newStr = "";
for (i = 0; i <= str.length; i++) {
if (str.charAt(i) != "a" || str.charAt(i) != "e" || str.charAt(i) != "i" || str.charAt(i) != "o" || str.charAt(i) != "u") {
newStr += str.charAt(i)
}
return newStr;
}
}
Expected: 'Ths wbst s fr lsrs LL!', instead got: 'T'
Why does my loop stops? It doesn't continue with i++? Probably beginner's mistake. Appreciate any help.
Share Improve this question edited Mar 5, 2019 at 21:19 ziggy wiggy 1,0677 silver badges6 bronze badges asked Mar 5, 2019 at 21:11 Marcel KredzelMarcel Kredzel 451 gold badge2 silver badges8 bronze badges 7-
1
Move the
return
outside of the loop. – Jonas Wilms Commented Mar 5, 2019 at 21:12 -
2
Also,
i <= str.length
would lead to an off-by-one error. – VLAZ Commented Mar 5, 2019 at 21:13 -
2
Also, your
||
should be&&
. – ziggy wiggy Commented Mar 5, 2019 at 21:13 -
3
Also, it can be more easily solved with a regular expression:
return str.replace(/[aeiou]/gi, '');
– Christoph Herold Commented Mar 5, 2019 at 21:15 -
function disemvowel(str){ return str.replace(/[aeiou]/gi, ''); }
– Ryan Wilson Commented Mar 5, 2019 at 21:15
4 Answers
Reset to default 2Some annotations:
- declare
i
, - loop until
i < str.length
, because arrays and strings are zero based, - take a string for checking a character with
String#includes
, - use a single character by taking a property accessor with the index, instead of
String.charAt
(it is shorter), - take a small letter case for checking
continue
thefor
statement, if a character is found,- move the
return
statement to the end of the function.
function disemvowel(str) {
var newStr = "",
i;
for (i = 0; i < str.length; i++) {
if ("aeiou".includes(str[i].toLowerCase())) continue;
newStr += str[i];
}
return newStr;
}
console.log(disemvowel("This website is for losers LOL!"));
let str = ('This website is for losers LOL!'.replace(/A|E|I|O|U|a|e|i|o|u/g, ''))
console.log(str)
function disemvowel(str) {
return str.match(/[^aeiou]/gi).join('');
}
My Solution !
function disemvowel(str) {
let newStr = (str.replace(/A|E|I|O|U|a|e|i|o|u/g, ''))
return newStr;
}
本文标签: Disemvowel TrollsJavascriptStack Overflow
版权声明:本文标题:Disemvowel Trolls - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742133232a2422255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论