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
 |  Show 2 more ments

4 Answers 4

Reset to default 2

Some 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 the for 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