0

I am trying to create a function pluralizeParam(n, word, pluralWord) with these requirements:

  • If n is 1, return the non-plural word (parameter word);otherwise, add an “s” to the plural word;
  • If the pluralWord parameter is provided, instead of adding an “s,” return the pluralWord.

What I have done so far is following:

function returnPluralWord(n, word, pluralWord) {
    if (n === 1) {
        return word;
    } else if (n === 1 && (word.lastIndexOf("s")) || (word.lastIndexOf("ess"))) {
        return word + "s";
    } else if (n !== 1 && word.length - 2 === "es") {
        return word + "s";
    } else if (pluralWord !== 'undefined') {
        return pluralWord;
    }
}

var result = returnPluralWord(2, "lioness", "lionesses");
console.log(result);

My problem is: it is not printing pluralWord. How do I do that? Thanks

1
  • 2
    Your second condition will never be executed; you've already handled n === 1, but more to the point, move your 4th case second. Commented Oct 12, 2017 at 17:16

1 Answer 1

2

word.length - 2 can't never be equal to "es". You need also to rearrange your statements, the 2nd is already catched by 1.

When you use the word.lastIndexOf('s') ( which is wrong logic I think ), it returns the last index of the s character, not if it ends on s.

You can check String#endsWith and String#startsWith methods, these ones check if the string starts or ends with the given part

const str = 'less';
console.log(str.endsWith('s'))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.