Challenge 21 Megathread

Welcome to the 21st and final challenge! Use this thread for any and all questions relating to challenge 21. Good luck!

Make sure to check your emails after you complete today’s challenge for a special offer. :raised_hands:

1 Like
const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  for (let recipe of recipes) {
    const a = ~bakeryA.indexOf(recipe.ingredients[0])
    const b = ~bakeryA.indexOf(recipe.ingredients[1])
    const c = ~bakeryB.indexOf(recipe.ingredients[0])
    const d = ~bakeryB.indexOf(recipe.ingredients[1])
    
    if ((a || b)  && (c || d)) {
      return recipe.name;
    }
  }
}
4 Likes
const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  for (let recipe of recipes) {
    let ingredient_a = recipe.ingredients[0];
    let ingredient_b = recipe.ingredients[1];
    
    if ((bakeryA.includes(ingredient_a) && bakeryB.includes(ingredient_b)) ||
        (bakeryA.includes(ingredient_b) && bakeryB.includes(ingredient_a))) {
      return recipe.name;
    }
  }
}
2 Likes

Hi,
I created the function ingredientCheck() as support function , but gives me an array like this ["0","1","2"]
this is my function:

const ingredientCheck = function(bakeryA, bakeryB, recipes){
  let recipe = [];
  
  for(var r in recipes){
    if(bakeryA.indexOf(r.ingredients)){
      
     recipe.push(r) ;
    }
    else if (bakeryB.indexOf(r.ingredients)){
      recipe.push(r);
    }
  }
   return recipe;
};
console.log(ingredientCheck(bakeryA,bakeryB,recipes));

Then I don’t know how implements other function,I mean should I use includes() ? If yes , how can I do?

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  // Code here!
  
  let ingredientA = ingredientCheck(); // ?
  let ingredientB = ingredientCheck();  // ?
  
  
}

------UPDATE------
Search on Internet I found a solution, but now give me []:

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  // Code here!
  //array1.filter(value => array2.includes(value))
  
  let ingredientA = recipes.filter(ingredients => bakeryA.includes(ingredients));
  return ingredientA;
  let ingredientB = recipes.filter(ingredients => bakeryB.includes(ingredients));
  return ingredientB;
  
};

EDIT: Never mind, issue was with the test cases. Solution passes now.

const chooseRecipe = (bakeryA, bakeryB, recipes) => recipes.find(recipe => recipe.ingredients.every(ingredient => bakeryA.concat(bakeryB).includes(ingredient))).name

Golfed version below:

chooseRecipe=(a,b,r)=>r.find(r=>r.ingredients.every(i=>a.concat(b).includes(i))).name
2 Likes

You could use Array#includes instead :wink:

Same problem occur. Pass the “Test Code” but failed the “Submit Code”

your code is high classed, i love that

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  // Code here!
  //array1.filter(value => array2.includes(value))
  
  var ingredientA = recipes.filter(function(ingredients){ 
  
  //return !unnecessaryWords.includes(currentWord);
  
  return !bakeryA.includes(ingredients);
    
  });
  var ingredientB = recipes.filter(function(ingredients){ 
  return !bakeryB.includes(ingredients);
 
  
});

Give me undefined :slightly_frowning_face:

Notice how we turn the bakery array into a dictionary to avoid expensive lookups later

const makeDic = A => A.reduce( (acc,next) => ({...acc, [next]: true}), {});

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  const [aDic, bDic] = [makeDic(bakeryA), makeDic(bakeryB)];
  return recipes.find( ({ ingredients }) => 
    aDic[ingredients[0]] && bDic[ingredients[1]] ||
    aDic[ingredients[1]] && bDic[ingredients[0]]
  ).name
}
1 Like

High five everyone for making it to the 21st day, the final day of the challenge.

I hope you learned a lot along the way, built consistent coding habits and inched closer to becoming a confident JavaScript developer.

Here’s a link to my final video in this series, tackling today’s challenge and solving it: https://youtu.be/NNMvXx8OV8Q

Finally, thanks to Lighthouse Labs for putting together such an amazing challenge and sharing it with the community.

5 Likes

I’m getting the same thing. My code passes in the “test code” but fails on “submit code”.

… nvm, found typo :sweat_smile:

const chooseRecipe = (bakeryA, bakeryB, recipes) =>
  recipes.find(
    ({ ingredients }) =>
      (bakeryA.includes(ingredients[0]) && bakeryB.includes(ingredients[1])) ||
      (bakeryA.includes(ingredients[1]) && bakeryB.includes(ingredients[0]))
  ).name;
1 Like

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
for (let recipe of recipes) {
let ingredient_b = recipe.ingredients[1];
let ingredient_a = recipe.ingredients[0];

if ((bakeryA.includes(ingredient_a) && bakeryB.includes(ingredient_b)) ||
    (bakeryA.includes(ingredient_b) && bakeryB.includes(ingredient_a))) {
  return recipe.name;
}

}
}

2 Likes

Thanks for the kind words, and for making such helpful videos for each day of the challenge!

Hey! Mine is very similar!

const chooseRecipe = (bakeryA, bakeryB, recipes) =>
  recipes.find(
    ({ ingredients }) =>
      ingredients.some(ingredient => bakeryA.includes(ingredient)) &&
      ingredients.some(ingredient => bakeryB.includes(ingredient))
  ).name
5 Likes

Using indexOf and booleans:

const chooseRecipe = function(bakeryA, bakeryB, recipes) {
  
  for(let recipe of recipes){
    let inBakeryA = false,
        inBakeryB = false;
    
    for(let ingredient of recipe.ingredients){
      if(bakeryA.indexOf(ingredient) >= 0){
        inBakeryA = true;
      }
      if(bakeryB.indexOf(ingredient) >= 0){
        inBakeryB = true;
      }
    }
    if(inBakeryA && inBakeryB){
      return recipe.name;
    }
  }
      
  
}
1 Like

When you do recipes.filter(ingredients…) here, ingredients is an object. What you want to access each object in recipes, then access the ingredients property(which is an array) in that object, then access each of the ingredients.

1 Like