How do I change the last letter of a sting based on a user prompt

Viewed 40

When I test the code it only runs the first if statement even when I change the other if statements to else if

// Returns the number and pluralized form, like "5 cats" or "1 dog", given
// a noun and count. However, there are a few exceptions. In this exercise,
// add "es" in the following cases:
// 1. the given noun ends with 'o'. For example, '5 potatoes'.
// 2. the noun ends in "f" or "fe" change the "f" to a "v" and add "-es."
// 3. the noun ends in "y", change the "y" to a "i" and add "-es."
// HINT: to replace the last character, you can use `replace()` with Regex.
// For example, if you would like to replace the last 'e' with 'o' in a string `str`,
// you can use `str.replace(/e$/, 'o')`.

const noun = prompt("Enter a noun");
const count = prompt("Enter a number");

console.log(noun);
console.log(count);

if ((count > 1) || (count < 0) || (count == 0) && noun.slice(-1) == /o$/) {
  result = count + " " + noun + "es";
} else if ((count > 1) || (count < 0) || (count == 0) && noun.slice(-1) == /f$/) {
  result = count + " " + noun.replace(/f$/, "v") + "es";
} else if ((count > 1) || (count < 0) || (count == 0) && noun.slice(-1) == /y$/) {
  result = count + " " + noun.replace(/y$/, "i") + "es";
} else(result = count + " " + noun)
// DO NOT CHANGE THIS.
console.log(result);

0 degrees
Failed

 
0 thieves
Failed

 
1 thief
Passed

 1 family
Passed

 
2 families
Passed


1 party
Passed


1 apple
Passed

2 apples
Failed


10 balloons
Failed


1 balloon
Passed

3 knife
Failed


2 potatoes
Passed

10 tomatoes
Passed


2 parties
Passed


-40 degrees
Failed
1 Answers

I don't completely understand what the problem is, so this code does what you asked for.

const noun = prompt("Enter a noun");
let count = prompt("Enter a number");
if (count > 1 || count <= 0){

if (noun.endsWith("o")) {
    console.log(`${count} ${noun}es`);
} else if (noun.endsWith("f")) {
    console.log(`${count} ${noun.replace(/f$/, "ves")}`);
} else if (noun.endsWith("fe")) {
    console.log(`${count} ${noun.replace(/fe$/, "ves")}`);
} else if (noun.endsWith("y")) {
    console.log(`${count} ${noun.replace(/y$/, "ies")}`);
} else {
    console.log(`${count} ${noun}s`);
}

}else{
console.log(`${count} ${noun}`);

}

Related