0

I'm trying to make a text randomizer, something that will create new sentences given certain words and where to place them. I've created arrays of nouns, verbs, amounts, etc. The problem is, I need it to be able to give different amounts of certain nouns (eg. one apple, two bananas), and I need it to match the amount to the singular or plural version of the word. I can't have it say, "one apples", or "two banana". I'm wondering the best way to do this. I thought about using an if/else statement but am not sure that's possible within document.write. I have a lot of places I would need to do this within a longer sentence, so I'm trying to keep from writing a million different arrays.

var amount = ["zero", "one", "two"]; 
var fruit = ["apple", "banana", "orange", "grape"]; 

var rand1 = [Math.floor ( Math.random() * amount.length )]; 
var rand2 = [Math.floor ( Math.random() * fruit.length )]; 

document.write("I have" + " " + amount[rand1] + fruit[rand2] + "."); 
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
Jason
  • 1

2 Answers2

3

Use the conditional operator. If the random amount is one, don't add an s, otherwise add an s:

var amount = ["zero", "one", "two"];
var fruit = ["apple", "banana", "orange", "grape"];

var rand1 = [Math.floor(Math.random() * amount.length)];
var rand2 = [Math.floor(Math.random() * fruit.length)];

document.write("I have" + " " + amount[rand1] + ' ' + fruit[rand2] + (amount[rand1] === 'one' ? '' : 's') + ".");

Might be more readable using template literals:

var amount = ["zero", "one", "two"];
var fruit = ["apple", "banana", "orange", "grape"];

var rand1 = [Math.floor(Math.random() * amount.length)];
var rand2 = [Math.floor(Math.random() * fruit.length)];

document.write(`I have ${amount[rand1]} ${fruit[rand2]}${amount[rand1] === 'one' ? '' : 's'}.`);

The expression

amount[rand1] === 'one' ? '' : 's'

means: if the expression amount[rand1] evaluates to one, the conditional expression as a whole evaluates to '', otherwise the whole conditional expression evaluates to ''. It's a more concise version of if/else when you need something to evaluate to one value or another.

Also note that document.write is rarely a good idea - if you want to show data to the user, consider inserting text into an element or using console.log or something like that. (See Why is document.write considered a "bad practice"?

CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
0

The answer from @CertainPerformance is very good - but keep in mind that the plural is not always just an added "s" - take "raspberry" / "raspberries" as example.

I usualy tend to add both versions to the array and choose which one to take according to the amount, something like this:

var amount = ["zero", "one", "two"];
var fruit = [
 ["apple","apples"],
 ["banana","bananas"],
 ["orange","oranges"],
 ["grape","grapes"],
 ["raspberry","raspberries"]
];

var rand1 = [Math.floor(Math.random() * amount.length)];
var rand2 = [Math.floor(Math.random() * fruit.length)];

document.write(`I have ${amount[rand1]} ${fruit[rand2][amount[rand1] === 'one' ? 0 : 1]}.`);
Ste Bächler
  • 468
  • 3
  • 10