Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
How many objects can I construct with given number of items

I have a number of ingredients and must know the maximum number of sandwiches (Big and small sandwiches) I can make with it. If ingredients remain, the result must be false.

  • Big sandwich : 4 slices of tomatoes and 2 slices of cheese
  • Small sandwich : 2 slices of tomatoes and 1 of cheese

My code works if all ingredients can be used with only small sandwiches or maximum one big sandwich. It also returns false if ingredients are remaining. However, it returns false if we need more than one big sandwich in order to use all ingredients.

var result = []
var ispossible = function(tomatoes, cheese) {

  /* we need in any case an even number of tomatoes slices */
  if (tomatoes % 2 === 0) {
    /* the easiest way is making as many small sandwiches as possible. The minimum number of big sandwiches is the number of cheese slices remaining when we devise the total number by 2  */
    var bigSandwich = cheese % 2
    var smallSandwichTomatoes = (tomatoes - 4 * bigSandwich) / 2
    var smallSandwich = cheese - bigSandwich
    console.log("we need" + smallSandwich + "small sandwiches and " + bigSandwich + "big sandwiches")
  } else {
    console.log("false, all the ingredients cannot be used")
  }
}

ispossible(10, 3)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assuming that this solution helps other readers with enhancing their rudimentary algorithm, programming skills:

const bigSmallSandwiches = (t = 10, c = 5) => (
  t % 2 !== 0 || t / c !== 2 ?
  'false, all the ingredients cannot be used' :
  'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);

Explanation

  1. If number of tomato slices is not 'even' or if number of cheese is not exactly half the number of tomato, return 'false'.
  2. Determine number of 'bigSandwiches' as tomatoes / 4
  3. Determine number of 'smallSandwiches' using remaining slices of tomatoes

Code-snippet:

const bigSmallSandwiches = (t = 10, c = 5) => (
  t % 2 !== 0 || t / c !== 2 ?
  'false, all the ingredients cannot be used' :
  'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);

[
  [20, 10],
  [8, 4],
  [4, 3],
  [14, 7]
].forEach(x => console.log('tomatoes: ' + x[0] + '\tcheese slices ' + x[1] + '\n' + bigSmallSandwiches(x[0], x[1])));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!