Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Problems/Remove-The-Last-Vowel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Remove-The-Last-Vowel

Write a function that removes the last vowel from each word in a sentence.

A vowel is one of: `a, e, i, o, u` (case-insensitive).

Each word in the sentence should be processed independently, and only the **last vowel** of each word should be removed.

Extra challenge (TypeScript/JavaScript):
Try to implement the solution as a **one-liner**, meaning the function should contain only a `return` statement using method chaining.

## Documentation

### Solution Idea

The sentence is first split into an array of words. Each word is then processed individually.

For every word, create a new variable that will store the modified version of that word.
Then iterate through the word from **right to left**, checking each character.

- If the character is **not a vowel**, add it to the new word.
- If the character **is a vowel**, check whether the new word currently matches the original word up to that position.
- If it matches, it means that no vowel has been removed yet, so we **skip this vowel** (this removes the last vowel).
- If it does not match, a vowel has already been removed earlier, so we **add the vowel** to the new word.

After processing the whole word, continue with the next word in the array.

Finally, join the modified words back together into a sentence and return the result.

---

### [Implementation](./solver.ts)

First, the sentence is split at each space `" "` to get an array of words.
Then we use `map` to process each word individually.

For each word, we split it into characters. To keep the number of methods small, we use `[...str]` to convert the word into an array of characters.

Next, we process this array using `reduceRight`.
We could also reverse the array and use `reduce`, but `reduceRight` saves us that extra step.

Inside the `reduceRight` function, we check whether the substring of the original word up to the current index matches the value that is currently being built (`current + previous`). If it matches, it means that **no vowel has been removed yet**.

- If the current character is a vowel, we skip it by returning only the previous value.
- If it is not a vowel, we return `current + previous`.

If the substring does **not** match anymore, it means a vowel has already been removed earlier, so we simply return `current + previous`.

After `reduceRight` finishes, we have the word without its last vowel.

Once the `map` function has processed all words, we join the array back into a sentence using a space `" "` and return the result.

---

### Information's

This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com).
[Post](https://slothbytes.beehiiv.com/p/5-things-tutorials-never-teach) from January 07, 2026.
28 changes: 28 additions & 0 deletions Problems/Remove-The-Last-Vowel/remove-the-last-vowel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import removeLastVowel from "./solver";

describe("Removes the last vowel for each word in each sentence", () => {
it("should return: 'Keybord'", () => {
expect(removeLastVowel("Keyboard")).toEqual("Keybord");
});

it("should return: 'Thos wh dar t fal miserbly cn achiev gretly.'", () => {
expect(removeLastVowel("Those who dare to fail miserably can achieve greatly.")).toEqual(
"Thos wh dar t fal miserbly cn achiev gretly."
);
});

it("should return: 'Lov s serios mentl diseas'", () => {
expect(removeLastVowel("Love is a serious mental disease.")).toEqual("Lov s serios mentl diseas.");
});

it("should return: 'Gt bsy livng r gt bsy dyng'", () => {
expect(removeLastVowel("Get busy living or get busy dying.")).toEqual("Gt bsy livng r gt bsy dyng.");
});

it("should return: 'f yo wnt t liv hppy lif, ti t t gol, nt t peopl.'", () => {
expect(removeLastVowel("If you want to live a happy life, tie it to a goal, not to people.")).toEqual(
"f yo wnt t liv hppy lif, ti t t gol, nt t peopl."
);
});
});
16 changes: 16 additions & 0 deletions Problems/Remove-The-Last-Vowel/solver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** biome-ignore-all lint/complexity/useIndexOf: Can't use here, searching in an array*/

const vowels: readonly string[] = ["a", "e", "i", "o", "u"];

export default function removeLastVowel(text: string): string {
return text
.split(" ")
.map((str) =>
[...str].reduceRight(
(preVal, curVal, curI) =>
str.substring(curI) === curVal + preVal && vowels.includes(curVal.toLowerCase()) ? preVal : curVal + preVal,
""
)
)
.join(" ");
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha

## Problems

Solved: **22 problems**
Solved: **23 problems**

🟢 Easy: 16
🟢 Easy: 17
🟠 Medium: 5
🔴 Hard: 1

Expand Down Expand Up @@ -38,6 +38,7 @@ Solved: **22 problems**
| [Reach Exit](./Problems/Reach-Exit/README.md) | 🟢 Easy |
| [One Two Skip a Few](./Problems/One-Two-Skip-A-Few/README.md) | 🟢 Easy |
| [Day number of year](./Problems/Day-Number-Of-Year/README.md) | 🟢 Easy |
| [Remove the last vowel](./Problems/Remove-The-Last-Vowel/README.md) | 🟢 Easy |

<!-- PROBLEMS_END -->

Expand Down