Ad

Jake's girlfriend just told him she wants to go gluten-free. And she wants him to build a Javascript function to help them detect gluten in their favorite foods. I know, the request is a little weird, but why not help him?

The code should take a list of ingredients and detect whether or not it contains the following:

  "wheat",
  "wheat flour",
  "triticale",
  "barley",
  "rye",
  "brewer's yeast",
  "malt",
  "wheatberries",
  "durum",
  "emmer",
  "semolina",
  "spelt",
  "farina",
  "farro",
  "graham",
  "kamut",
  "einkorn"
  
function glutenDetector(ingredients){
  var gluten = [
  "wheat",
  "wheat flour",
  "triticale",
  "barley",
  "rye",
  "brewer's yeast",
  "malt",
  "wheatberries",
  "durum",
  "emmer",
  "semolina",
  "spelt",
  "farina",
  "farro",
  "graham",
  "kamut",
  "einkorn"
  ];
  var glutenDetect = false; 
  var ingredientsList = ingredients.split(',');
  ingredientsList.forEach(function(ingredient){
    var ingredientClean = ingredient.trim().toLowerCase();
    gluten.forEach(function(glutenIngredient){
      if(ingredientClean.indexOf(glutenIngredient) !== -1){
        glutenDetect = true;
      }
    });

  });

  return glutenDetect;
  
}
Arrays
Data Types
Map/Reduce
Algorithms
Logic

For loops are commonly overused when other Javascript methods can lead to a cleaner and faster solution. A common example would be finding duplicates in an array.

This function called duplicates takes an array of numbers and returns a new array with the numbers duplicated in the original array ordered by value.

Your goal is to refactor this code to still find duplicates in an array and return those duplicates in a new array, but no longer use a for loop.

Note: numbers and their corresponding string representations should not be treated as duplicates (i.e., '1' !== 1).

Based on http://www.codewars.com/kata/find-duplicates/javascript

function duplicates(arr) {
  var out = [];  
  for(var x=0;x<arr.length-1;x++)
  {
    var ch = arr[x];
    for(var y=x+1;y<arr.length;y++)
    {
      var comp = arr[y];
      if (comp === ch && out.indexOf(comp) === -1)
      {
        out.push(comp);
        break;
      }
    }
  }
  out.sort();
  return out;
}

There is a FizzBuzzy Race coming up. There will be a cone dropped at every mile marker divisable by 3 and 5, start at the start line.

When divisable by 3 and 5 there will be two cones dropped.

The start line is the 0th point and needs cones also.

The rules of the Fizz Buzzy race require that the instructions for placing the cones must not contain "if" because it angers the goblin who places them.

The command will be: 'fizzbuzzy' and take the miles as the sole parameter.

There is a FizzBuzzy Race comming up. There will be a cone dropped at every mile marker divisable by 3 and 5, start at the start line.

When divisable by 3 and 5 there will be two cones dropped.

The start line is the 0th point and needs cones also.

The command will be: 'fizzbuzzy' and take the miles as the sole parameter.

Code
Diff
  • function fizzbuzzy(n) {
      var cones = 1;
      for (i = 0; i < n; i++){
    
        if (i % 3 === 0 && i % 5 === 0){
         cones += 2;
        } else if (i % 5 === 0){
          cones++;
        } else if (i % 3 === 0){
            cones++;
        }
             
      }
      return cones;
    }
    
    • function fizzbuzzy(n) {
    • //insert code here
    • }
    • var cones = 1;
    • for (i = 0; i < n; i++){
    • if (i % 3 === 0 && i % 5 === 0){
    • cones += 2;
    • } else if (i % 5 === 0){
    • cones++;
    • } else if (i % 3 === 0){
    • cones++;
    • }
    • }
    • return cones;
    • }