Replace vowels with numbers in javascript

I am trying to write a function that will remove all vowels in a given string in JS. I understand that I can just write string.replace[/[aeiou]/gi,""] but I am trying to complete it a different way...this is what I have so far... thank you!

I first made a different function called IsaVowel that will return the character if it is a vowel...

function withoutVowels[string] {

var withoutVowels = "";
for [var i = 0; i < string.length; i++] {
    if [isaVowel[string[i]]] {
 ***not sure what to put here to remove vowels***
       }
  }
    return withoutVowels;
}

asked May 19, 2017 at 22:11

2

Use accumulator pattern.

function withoutVowels[string] {

  var withoutVowels = "";
  for [var i = 0; i < string.length; i++] {
      if [!isVowel[string[i]]] {
        withoutVowels += string[i];
      }
    }
    return withoutVowels;
}

function isVowel[char] {
  return 'aeiou'.includes[char];
}

console.log[withoutVowels['Hello World!']];

answered May 19, 2017 at 22:15

EyuelDKEyuelDK

2,8192 gold badges18 silver badges25 bronze badges

I tried doing this problem by first splitting the string into an array, while also creating an array of vowels. Then go through each element in the string array and check whether it's in my vowel array. If it is not in my vowel array, push it to the withoutVowels array. At the end of the for loop, join all elements in the withoutvowels array and return.

function withoutVowels[string] {
            var strWithoutVowels =  [];
            string = string.split[''];
            var vowels = ['a', 'e', 'i', 'o', 'u'];
            for [var i = 0; i < string.length; i++] {
                if [vowels.indexOf[string[i]] < 0] {
                    strWithoutVowels.push[string[i]]
                }
            }
            strWithoutVowels = strWithoutVowels.join[''];
            return strWithoutVowels;
        }
        console.log[withoutVowels['Hello World!']]

answered May 19, 2017 at 22:24

FroyFroy

6741 gold badge5 silver badges15 bronze badges

Remove the Vowels from a String #

To remove the vowels from a string, call the replace[] method with the following regular expression - /[aeiou]/gi, e.g. str.replace[/[aeiou]/gi, '']. The replace[] method will return a new string where any vowel in the original string is replaced with an empty string.

Copied!

const str = 'hello world'; const noVowels = str.replace[/[aeiou]/gi, '']; console.log[noVowels]; // 👉️ hll wrld

We passed the following 2 parameters to the String.replace method:

  1. a regular expression to match in the string
  2. the replacement for each match

The forward slashes / / mark the beginning and end of the regular expression.

The part in the square brackets [] is called a character class and matches any of the characters in the brackets, in our case - any vowel.

For example, [abc] matches the characters a, b and c.

We used the g [global] flag because we want to match all occurrences of a vowel in the string and not just the first occurrence.

The i flag is used to make the search case insensitive. These two regular expressions are the same:

  • /[aeiou]/gi
  • /[aeiouAEIOU]/g

If you need a regex cheatsheet, check out this one from MDN.

The second parameter we passed to the replace[] method is the replacement string for each match. Because we want to remove each vowel, we replace it with an empty string.

Note that the replace[] method doesn't change the original string, it returns a new string. Strings are immutable in JavaScript.

Further Reading #

  • Increment a Value in an Object using JavaScript
  • Initialize an Array of Boolean Values in JavaScript

How do you replace a vowel in a string with numbers?

Method #1 : Using loop + replace[] Initially create a string of vowels and then iterate through the given test_str, on detecting a vowel in test_str replace the vowel with K using replace[] method.

How do you remove a vowel from a string?

The algorithm is as follows;.
Algorithm. START Step-1: Input the string Step-3: Check vowel presence, if found return TRUE Step-4: Copy it to another array Step-5: Increment the counter Step-6: Print END. ... .
Example. ... .
Output..

How do you print a vowel in Javascript?

Printing vowels in the string.
let s = "welcome".
let c, i..
for [i=0; i

Chủ Đề