Turning plural values to singular and singular values to plural!?

https://codepen.io/TuscannyCodes/pen/jOVevxZ

Hi, im curious as to how I can make a rule for my game that allows a value of grape to = grapes and vice versa. Ive been trying to figure it out but havent come up with a decent solution. As it stands, I have created individual responses, one for grape, and another for grapes.

This is a problem because if the user inputs grape when the value should be grapes, the code won’t run correctly. I hope this is making sense. In my code, Ive commented out the majority of the code so you can just see the parts that im talking about for better clarification.

I want to know how to fix this not just for the sake of the game, but also for the sake of knowing how to do something like this. What would be a proper solution?

Any help is greatly appreciated.

multiple identical switch statements

case "grape": 
case "grapes": 
alert(`"it takes about 2.5 pounds of grapes to make one bottle of wine."`)
break;

1 Like

hmmm.This makes sense, but I tried it out but its breaking the code. How would the switch statement know that the case is the randomFruit value?

Im sure im missing something :sweat_smile:

with the value you put at the beginning when you write switch(randomFruit) I imagine

1 Like

you can’t put cases on their own

you can put two comparisons in the same if statement separated by the OR operator

1 Like

The problem that Im encountering is in my array of possible answers, I have grape AND grapes. I did this because with grape(s) its likely that a user may not just enter grape, but enter grapes instead.

So im thinking I need a way to somehow allow an input of ‘grapes’ or ‘grape’ to equal to the same value of grape in my array. If that makes sense.

you can have an array ["grape", "grapes"]

you could have an array of regular expressions to check the input answer with

anyway, you need to refactor pretty much everywhere

1 Like

Since you already have the basics of the game working now, I will share a refactor that incorporates the checking for a plural ending of a fruit name. The only catch with my logic is that it only works for fruit names that just add extra letters to the original fruit name. The code below fairly DRY.

// Fruit guessing game

const fruits = [
  {
    name: "banana",
    pluralEnding: 's',
    triesTillAlert: 3,
    hint: 'Humans share about 50% of our DNA with this fruit.'
  },
  {
    name: "apple",
    pluralEnding: 's',
    triesTillAlert: 4,
    hint: 'A city is nicknamed after this fruit.'
  },
  {
    name: 'kiwi',
    pluralEnding: 's',
    triesTillAlert: 3,
    hint: 'This fruit is fuzzy on the outside.'
  },
  {
    name: 'pear',
    pluralEnding: 's',
    triesTillAlert: 5,
    hint: 'Sounds like "pair".'
  },
   {
    name: 'pineapple',
    pluralEnding: 's',
    triesTillAlert: 3,
    hint: 'This fruit is not a pine, or an apple.'
  },
   {
    name: 'peach',
    pluralEnding: 'es', 
    triesTillAlert:4 ,
    hint: 'The state of Gorgia is nicknamed after this fruit.'
  },
   {
    name: 'orange',
    pluralEnding: 's',
    triesTillAlert: 4,
    hint: 'This fruit makes the most popular fruit juice in America.'
  },
   {
    name: 'papaya',
    pluralEnding: 's',
    triesTillAlert: 5,
    hint: 'This fruit has many black seeds in the middle.'
  },
   {
    name: 'tomato',
    pluralEnding: 'es',
    triesTillAlert: 4,
    hint: 'This fruit is red, and often mistaken for a vegetable.'
  },
  { 
    name: 'grape',
    pluralEnding: 's',
    triesTillAlert: 4,
    hint: 'Some people squish this fruit with their feets.'
  }
];

function fruitGuessingGame() {
  let tries = 0;
  let randomNum = Math.floor(Math.random() * fruits.length);
  let randomFruit = fruits[randomNum];
  console.log(randomFruit); //Here is the answer
  let fruitGuess;
  let winner = false;
  do {
    fruitGuess = prompt('What fruit am I thinking of?');
    if (fruitGuess === null) { // in case Cancel is clicked
      break;
    }
    tries++;
    const convertGuess = fruitGuess.toLowerCase();
    const pluralName = randomFruit.name + randomFruit.pluralEnding;
    if (convertGuess !== randomFruit.name && convertGuess !== pluralName) {
      let alertMessage = 'Try again.';  
      if (tries >= randomFruit.triesTillAlert) {
        alertMessage += ' Hint: ' + randomFruit.hint;
      }
      alert(alertMessage);
    } 
    else {
      alert('YOU WIN! It only took you ' + tries + ' tries to guess!');
      winnner = true;
      break;
    }
  } while (!winner);
}

EDIT: This code was based on your original code which only showed the hint after a certain number of tries.

1 Like

WOW so instead of the fruit being just strings in an array, In this example the fruit values are objects and the plural values are appended in the function! VERY sophisticated. Im going to continue learning before I implement this technique so I’ll fully know how everything works but this gives me a great overview on how the code can operate at a higher level!

Really great stuff! THANK YOU! :+1:

One more variant for you to study. This version is more like your current version in the sense that you get a hint immediately. The difference is you can specify as many hints as you want. The user will get a new hint (based on the order you create them in the hints array of each fruit object). Once there are no more hints, it tells the user this.

// Fruit guessing game

const fruits = [
  {
    name: "banana",
    pluralEnding: 's',
    hints: [
      'Humans share about 50% of our DNA with this fruit.',
      'The color of this fruit is yellow.',
      'The name of this fruit starts with the letter B and ends with the letter A.'
    ]
  },
  {
    name: "apple",
    pluralEnding: 's',
    hints: [
      'A city is nicknamed after this fruit.',
      'The color of this fruit is red.',
      'The name of this fruit starts with the letter A and ends with the letter E.'
    ]
   
  },
  {
    name: 'kiwi',
    pluralEnding: 's',
    hints: [
      'This fruit is fuzzy on the outside.',
      'The color of this fruit is green.',
      'The name of this fruit starts with the letter K and ends with the letter I.'
    ]
  },
  {
    name: 'pear',
    pluralEnding: 's',
    hints: [
      'This fruit name has a teardrop shape.',
      'The color of this fruit is yellow.',
      'The name of this fruit starts with the letter P and ends with the letter R.'
    ]
  },
   {
    name: 'pineapple',
    pluralEnding: 's',
    hints: [
      'The fruit is not a pine, or an apple.',
      'The fruit has a prickly outer shell.',
      'The name of this fruit starts with the letter P and ends with the letter E.'
    ] 
  },
   {
    name: 'peach',
    pluralEnding: 'es', 
    hints: [
      'The state of Gorgia is nicknamed after this fruit.',
      'The fruit is fuzzy on the outside.',
      'The name of this fruit starts with the letter P and ends with the letter H.'
    ] 
  },
   {
    name: 'orange',
    pluralEnding: 's',
    hints: [
      'This fruit makes the most popular fruit juice in America.',
      'The fruit is fuzzy on the outside.',
      'The name of this fruit starts with the letter P and ends with the letter H.'
    ]
  },
   {
    name: 'papaya',
    pluralEnding: 's',
    hints: [
      'This fruit has many black seeds in the middle.',
      'The fruit is the color of orange on the inside.',
      'The name of this fruit starts with the letter P and ends with the letter A.'
    ]
  },
   {
    name: 'tomato',
    pluralEnding: 'es',
    hints: [
      'This fruit is often mistaken for a vegetable.',
      'The color of this fruit is red.',
      'The name of this fruit starts with the letter T and ends with the letter O.'
    ]
  },
  { 
    name: 'grape',
    pluralEnding: 's',
    hints: [
      'Some people squish this fruit with their feet.',
      'The color of this fruit is purple.',
      'The name of this fruit starts with the letter G and ends with the letter E.'
    ]
  }
];

function fruitGuessingGame() {
  let tries = 0;
  let randomNum = Math.floor(Math.random() * fruits.length);
  let randomFruit = fruits[randomNum];
  console.log(randomFruit.name); //Here is the answer
  let fruitGuess;
  let winner = false;
  do {
    fruitGuess = prompt('What fruit am I thinking of?');
    if (fruitGuess === null) { // in case Cancel is clicked
      break;
    }
    tries++;
    const convertGuess = fruitGuess.toLowerCase();
    const pluralName = randomFruit.name + randomFruit.pluralEnding;
    if (convertGuess !== randomFruit.name && convertGuess !== pluralName) {
      let alertMessage = 'Try again.';  
      if (tries <= randomFruit.hints.length) {
        alertMessage += ' Hint: ' + randomFruit.hints[tries - 1];
      } else {
        alertMessage += ' Sorry, there are no more hints.';
      }
      alert(alertMessage);
    } 
    else {
      alert('YOU WIN! It only took you ' + tries + ' tries to guess!');
      winnner = true;
      break;
    }
  } while (!winner);
}