Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda