1

I was wondering how can you combine two variables (or more) that have the same string values, but different numbers.

For example, if you're combining a list of ingredients that are present in two different recipes. For a shopping list, like:

let ingredient1 = 1 + " apple";
let ingredient2 = 2 + " apple";
//combining ingredient1 and ingredient 2 would produce something like totalIngredients = 3 apples;

I can kind of figure out the pluralization, but I can't figure out how I can combine those two strings and for it to only increase the number if they're matching.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • 2
    Don't try to store everything as a string. Create an object that stores quantity and units in separate fields, then write code to add them (making sure the units match) and pluralize the units. – kindall Nov 08 '21 at 20:14
  • In addition to the above comment, you can also take some inspiration from this question: https://stackoverflow.com/questions/27194359/javascript-pluralize-an-english-string – smac89 Nov 08 '21 at 20:18
  • Oh yeah, I hadn't thought about that. But in general, is that even possible to achieve, if you were using strings? – Ivan Gospodinov Nov 08 '21 at 20:21
  • can you do that using just strings? yes. should you? god no. – szaman Nov 08 '21 at 20:36
  • but if you really need to, you could probably use a regular expression to find the ingredient type and count. something like `foo.match(/(\d*) (\w*)/)` – szaman Nov 08 '21 at 20:41

3 Answers3

2

Like others have noted, you should store your ingredients as objects. One way to achieve this is to have a class which stores the count and ingredient type. You can then define a function that checks for a given type and returns the count of ingredients.

class Ingredient {  
  constructor(count, type) {
    this.count = count;
    this.type = type;
  }
};

const countByType = (type) => ingredients.reduce((sum, ingredient) => {
  if (ingredient.type === type) {
    return sum += ingredient.count;
  }

  return sum;
}, 0);

const ingredients = [];
ingredients.push(new Ingredient(1, "apple"));
ingredients.push(new Ingredient(2, "apple"));
ingredients.push(new Ingredient(5, "orange"));



console.log(`${countByType("apple")} apples`);
console.log(`${countByType("orange")} oranges`);

If you prefer, you can achieve the same without classes as well:

const countByType = (type) => ingredients.reduce((sum, ingredient) => {
  if (ingredient.type === type) {
    return sum += ingredient.count;
  }

  return sum;
}, 0);

const ingredients = [];
ingredients.push({count: 1, type: "apple"});
ingredients.push({count: 2, type: "apple"});
ingredients.push({count: 5, type: "orange"});



console.log(`${countByType("apple")} apples`);
console.log(`${countByType("orange")} oranges`);
szaman
  • 2,159
  • 1
  • 14
  • 30
0

You can't combine both ingredients and it should make a calculation because let ingredient1 = 1 + " apple"; produces a string 1 apple

You can do something like this:

let ingredient = 'apple'
let ingredient1 = 1
let ingredient2 = 2
let totalIngredients = ingredient1 + ingredient2 + ingredient

Or maybe use a for loop to loop through all ingredients and add it to the total and then add 'apple'

Shmiel
  • 1,201
  • 10
  • 25
0

How can I sum numbers which are substrings in variables?

By converting the substrings to numbers. If the numbers are at the start of the string you can just use parseInt.

parseInt(ingredient1) + parseInt(ingredient2)

To check the types, you could use for example

let ingredient1 = 1 + " apple";
let ingredient2 = 2 + " apple";
let type1 = ingredient1.slice(ingredient1.indexOf(' ') + 1);
let type2 = ingredient2.slice(ingredient2.indexOf(' ') + 1);
if (type1 === type2) {
  console.log(`${parseInt(ingredient1) + parseInt(ingredient2)} ${type1}`);
}
MikeM
  • 13,156
  • 2
  • 34
  • 47