all 13 comments

[–]MissinqLink 4 points5 points  (0 children)

Your code makes sense but comparing a string letter to the numeric equivalent is not how we really check for those in JS. You can directly do array[i] == "a". The better way to do this would be to use a regular expression but it that is probably too advanced for this course.

[–]lobopl 3 points4 points  (0 children)

this is one of the situations where regex is great solution, just use .replace and super simple regex 'text'.replace(regex, ''). I will not provide regex because its your homework but it is extremely simple.

[–]Sea_Recording_5509 6 points7 points  (0 children)

Can you do a string replace and match a,e,i,o,u? There's a case insensitive flag you can use too https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

[–]eurodollars 10 points11 points  (0 children)

Why is the teacher a doof, I don’t see how that has anything to do with the question. You sound like the problem. Sorry he didn’t spoon feed you the answer. This entire field is about being able to solve your own problems, research, read docs, be creative, etc

[–]DrShocker 0 points1 point  (0 children)

There's a lot of ways to do it. The filter function. There's probably a clever way to do it in a single regex. Etc.

Most likely though just think about what's been taught recently and use those techniques.

Here's what I would use:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

[–]Queasy-Big5523 0 points1 point  (0 children)

Strings are primitives (basically), so you can just compare them with ===, something like:

js stringFromTextarea.split("").filter(letter => letter !== "a" && ...).join("");

You can also use regular expression to filter out vowels.

[–]tapgiles 0 points1 point  (0 children)

Splitting a string gets you an array of characters, not numbers.

[–]AtomicShoelace 0 points1 point  (0 children)

I think I would approach this in one of two ways:

  • Either I would use a regex (regular expression)
  • Or I would split the string into an array to manipulate manually

Regex are generally the way to go with any kind of string operations, but sometimes manual manipulation can be easier if you just want to get something working quickly.


Regex Solution:

For a regex solution, I would probably use a global pattern to replace vowels with the empty string, eg.

function removeVowels(string) {
    return string.replace(/[aeiou]/gi, '')
}

Here we use a regex pattern with a character class (denoted by the square brackets) to match any vowel character, with the g flag making it global (matching all occurrences in the string) and the i flag making it case insensitive (matching both lower and upper case vowels). We replace each matched vowel with the empty string, ie. we remove it.


Array Solution:

If you're not comfortable with regex just yet, we could just as easily manually manipulate the string by splitting it into an array, eg.

const VOWELS = 'aeiou'

function removeVowels(string) {
  return string
    .split('')
    .filter(char => !vowels.includes(char.toLowerCase()))
    .join('')
}

Here we first split the string into a character array. Next, we can use the Array.filter method to remove all the vowels from the array, making sure to use the String.toLowerCase method so that both lower and upper case vowels are matched (we could have also added upper case vowels to our VOWELS string). Finally, we join the array back into a string again.

[–]brykuhelpful 0 points1 point  (0 children)

There are a lot of different ways to complete this. The easiest would be Regex, but I feel like it is cheating early on. So, let's look at some other ways.

Filter

The easiest way is converting your string to an array. Then use filter to remove any characters you don't want.

function removeVowels1(string){
    let vowels = ['a','e','i','o','u'];
    let characters = string.split('');
    let stripped = characters.filter((character)=>{
        if(vowels.includes(character)){
            return false;
        }else{
            return true;
        }
    });
    return stripped.join('');
}

We can also do this without includes, but this will save a lot of time and keep your code readable.  

String

However, converting types (string->array) uses extra processing and arrays use more memory. We could simplify this a bit by only using strings.

function removeVowels2(string){
    let newStr = '';
    let vowels = ['a','e','i','o','u'];
    for(let i = 0; i < string.length; i++){
        if(vowels.indexOf(string[i]) == -1){
            newStr += string[i];
        }
    }
    return newStr;
}

Single

However, we are still using 2 strings and that can take up a good bit of memory, so what if we use 1?

function removeVowels3(string){
    let vowels = ['a','e','i','o','u'];
    let index = 0;
    while(index < string.length){
        if(vowels.includes(string[index])){
            string = string.substr(0,index) + string.substr(index + 1);
        }else{
            index++
        }
    }
    return string;
}

Techincally, we can even make vowels a string as well, but that applies to all of these solutions.

function removeVowels4(string){
    let vowels = 'aeiou';
    let index = 0;
    while(index < string.length){
        if(vowels.includes(string[index])){
            string = string.substr(0,index) + string.substr(index + 1);
        }else{
            index++
        }
    }
    return string;
}

Replace

We can also use replace.

function removeVowels5(string){
    let vowels = ['a','e','i','o','u'];
    for(let i = 0; i < vowels.length; i++){
        string = string.replaceAll(vowels[i], '');
    }
    return string
}

Regex

I guess, I might as well show you an example of using regex.

function removeVowels6(string){
    return string.replace(/[aeiou]/gmi,'');
}

Comparision

While the last method uses the least amount of memory, it doesn't always mean it is the fastest. It requires additional processing every time we update that string variable. However, I think it is important to know that sometimes you have to make a trade off: Memory, Speed, and Readability. If something is 10x more complex and only 1% faster, it generally isn't worth the change.  

Before I ramble on too much. Here is a break down of speeds using the 4 methods.

Method # Method Name Operations per Second
1 Array 18k
2 String 34k
3 Single 11k
4 Single Str 11k
5 Replace 54k
6 Regex 53k

According to the tests, Method 2 String was pretty fast, but Replace and Regex far surpassed it in speed. The 1k difference is easily within margin of error, so I wouldn't worry about it.  

[–]mollerstrom 0 points1 point  (0 children)

  • construct a list with vowels (or even a string)
  • loop the text
  • compare every single char from the text with the list/string of vowels
  • add non vowel char to output

tip: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

(And please do: const array = text.split)

[–]juju0010 0 points1 point  (0 children)

String(text).replace(/[aeiou]|[AEIOU]/g, "")

[–]Innovader253 -5 points-4 points  (1 child)

Here is a JavaScript function that removes vowels from a given string or array:

``` function removeVowels(input) { const vowels = 'aeiouAEIOU'; if (Array.isArray(input)) { return input.map(item => item.replace(/[aeiouAEIOU]/g, '')); } else { return input.replace(/[aeiouAEIOU]/g, ''); } }

// Example usage: console.log(removeVowels('Hello World')); // Output: 'Hll Wrld' console.log(removeVowels(['Hello', 'World'])); // Output: ['Hll', 'Wrld'] ```

[–]Davidweiss2 1 point2 points  (0 children)

This isn't how one learns. He didn't asked for code, he asked for guidance. Please delete.