I am new to JS. Need advice on this work

const noun = prompt(“Enter a noun”);
const count = prompt(“Enter a number”);


// Returns the number and pluralized form, like “5 cats” or “1 dog”, given
// a noun and count. Assume there are no irregular plural nouns.
let result = undefined;


I’m not sure what the question or problem is?

The comments are the instructions. I don’t know what the correct code for:
let result =
Then I don’t know what goes in after that. Would it be an if else? So that it returns a number and pluralized form of a noun. As I mentioned I am a true beginner at this.

Template Literals

Use the link above to learn a little more about template literals. You can assign the template let eral to the result.

1 Like

You could use + to put strings together in JavaScript.
"dog" + "s" would be "dogs"
So if you have "dog" in the variable noun
and 5 in the variable count
count + " " + noun + "s"
would be
"5 dogs"

jan, thank you for the reply. What about if else statements? How would I do that?

You can add a character in the same way that @janbazant1107978602 described, but within an if statement that checks for the count to be greater than 1

let pluralizedNoun = noun;
 pluralizedNoun += "s";

Thanks. What about this?

This is the second part

For plurals, you could check the last letter of noun using noun[noun.length - 1]
And you could use the .substring if you only want part of a string, like “up to the next-to-last letter”.

let str = "potato"; console.log(str); let plural; if (str[str.length - 1] == 'o') { plural = str + "es"; } else if (str[str.length - 1] == 'y') { plural = str.substring(0, str.length - 1) + "ies"; } else if (str[str.length - 1] == 'f') { plural = str.substring(0, str.length - 1) + "ves"; } else if (str.substring(str.length - 2) == "fe") { plural = str.substring(0, str.length - 2) + "ves"; } else { plural = str + "s"; } console.log(plural);

Note better string methods [than what I used in the Codebyte] exist, like .slice or .endsWith or .replace

Also, you can use && or || to combine conditions ( booleans ) if you need to do so.

I appreciate the help jan.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.