如何根据用户提示更改刺痛的最后一个字母



当我测试代码时,它只运行第一个if语句,即使我将其他if语句更改为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

我不完全理解问题是什么,所以这段代码符合您的要求。

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}`);
}

相关内容

  • 没有找到相关文章

最新更新