Click here to Skip to main content
15,663,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem: Write a function abbreviate(word) that takes in a string arg. The function should return a new string where all of its vowels are removed.

I can use RegEx to do this very simply, however, I am supposed to use a For Loop and it is Driving me absolutely insane. It feels like the Javascript .includes is written backwords and then the .slice doesn't seem to work in the way I would think.

What I have tried:

function abbreviate(word){
    vowels = "aeiou";
    for( let i = 0; i < word.length; i++){
        let letter = word[i]
            firstPart = word.slice(letter, i);
            secondPart = word.slice(i, letter);
            word = firstPart + secondPart;

It turns out the problems answer didn't even use slice after I spent 3 + Hours working on it. X.X Absolutely horrifying. The answer on the challenge ended up being:

let abbreviate = function(word) {
    let vowels = "aeiou";
    let newWord = "";

    for (let i = 0; i < word.length; i++) {
        let char = word[i];
        if (!vowels.includes(char.toLowerCase())) {
            newWord += char;

    return newWord;

console.log(abbreviate('wonderful')); // 'wndrfl'
console.log(abbreviate('mystery')); // 'mystry'
console.log(abbreviate('Accordian')); // 'ccrdn'
Updated 27-Sep-22 22:56pm

Instead of repeatedly using a slice, use the JavaScript Array from() Method[^] to convert the string to a character array, or use the JavaScript String charAt() Method[^] to access each character separately, and then build a new string without the vowels.

And return the new string, instead of printing it ...
Share this answer
Chris Aug2022 28-Sep-22 3:28am    
The console.log was so I could see what was being done inside the for loop through iterations.
I have to use the slice function. :(
There has to be a way to slice all letters BEFORE the vowel and all letters AFTER the Vowel, return them and then rerun the loop. That's the goal here. I know of other ways to do it but, again, I have to use the slice function. :(
OriginalGriff 28-Sep-22 3:58am    
The problem with the slice is that i changes each time round the loop, but the index you need doesn't.
So when you try the second slice, the value of i is already greater than the index of the character in the string:
BEAD  - the "E" is at index i = 1

After the slices word is BAD, but the next time round the loop i is 2, which means it ignores the "A" and looks at the "D".

Using an array is a load easier, but if you keep a "last index" carefully, you can slice out the "good" bits and add them to a different string as you go along.

Give it a try on paper, and you will see what I mean.

Messy though - and array makes it a lot cleaner!
Chris Aug2022 28-Sep-22 4:56am    
Could you give me a function that implements that? I finally looked at the answer to the problem and it doesn't even use SLICE! GAH. I've literlly spent 3 + hours on this(I've only been learning Javascript for th last few days in terms of coding challenges.)

I updated the question to have the answer frm the challenge. X.X Absolutely outrageous.
OriginalGriff 28-Sep-22 5:25am    
I told you that slice was a poor idea! :laugh:
Chris Aug2022 28-Sep-22 5:29am    
Yeah, I could definitely tell(during and after), however, the last 4 problems used slice and it was the end of the section so I assumed they wanted me to use slice. I could've done it with regex and saved all that time and had a much shorter answer than theirs.
First problem: your arguments to the slice method[^] are wrong. The first argument needs to be the start index, and the second argument is the end index.
firstPart = word.slice(0, i - 1);
secondPart = word.slice(i + 1);

Secondly, when you're using a for loop to iterate over a sequence, and you're modify that sequence within the loop, you need to iterate backwards; otherwise, you end up missing items.

Consider the input "feed":
  • word[0] === "f" is not a vowel; the word is unchanged.
  • word[1] === "e" is a vowel; the word is changed to "fed".
  • word[2] === "d" is not a vowel; the word is unchanged.

The end result is "fed", which is obviously not what you wanted.

If you reverse the iteration - for (let i = word.length - 1; i >= 0; i--):
  • word[3] === "d" is not a vowel; the word is unchanged.
  • word[2] === "e" is a vowel; the word is changed to "fed".
  • word[1] === "e" is a vowel; the word is changed to "fd".
  • word[0] === "f" is not a vowel; the word is unchanged.

The end result is now "fd".
Share this answer
Chris Aug2022 28-Sep-22 3:37am    
When I changed the code to reflect what you wrote I get:

Richard Deeming 28-Sep-22 3:47am    
You're getting undefined because your function doesn't return anything.
Richard Deeming 28-Sep-22 3:50am    
You'll also need to check whether you're removing the first letter:
let firstPart = i === 0 ? "" : word.slice(0, i - 1);

Chris Aug2022 28-Sep-22 3:57am    
Can you write the full, working function out for me? None of this seems to have worked and it will be easier for me to deconstruct it.
Richard Deeming 28-Sep-22 3:59am    
I already have! Click on the Demo link in my previous comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900