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;
}
var ingredientsCrackers = "ENRICHED FLOUR (WHEAT FLOUR, NIACIN, REDUCED IRON, THIAMIN MONONITRATE [VITAMIN B1], RIBOFLAVIN [VITAMIN B2], FOLIC ACID), SOYBEAN AND PALM OIL WITH TBHQ FOR FRESHNESS, WHOLE WHEAT FLOUR, SKIM MILK CHEESE (SKIM MILK, WHEY PROTEIN, CHEESE CULTURES, SALT, ENZYMES, ANNATTO EXTRACT FOR COLOR), CONTAINS TWO PERCENT OR LESS OF SALT, PAPRIKA, YEAST, PAPRIKA OLEORESIN FOR COLOR, SOY LECITHIN. CONTAINS WHEAT, MILK AND SOY INGREDIENTS";
var ingredientsCookies = "RICE FLOUR, EGGS, SUGAR, BUTTER, XANTHAN GUM";
describe("Solution", function(){
it("Test these crackers for gluten", function(){
Test.assertEquals(glutenDetector(ingredientsCrackers), true, "Uh oh these crackers totally have gluten!");
});
it("Test these cookies for gluten", function(){
Test.assertEquals(glutenDetector(ingredientsCookies), false, "Actually these cookies do not have gluten");
});
});
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;
}
Test.assertEquals(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]).toString, [1,3,4].toString, "Sorry, your array does not have the correct contents");
Test.assertEquals(duplicates([0, 1, 2, 3, 4, 5]).toString, [].toString, "Sorry, your array does not have the correct contents");
Test.assertEquals(duplicates.toString().indexOf("for"), -1, "You can't use for loops");
Test.assertEquals(duplicates.toString().indexOf("forEach"), -1, "You can't use for loops");
Test.assertNotEquals(duplicates.toString().indexOf("reduce"), -1, "You should be using reduce in your solution");
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.
Test.assertEquals(fizzbuzzy(21), 13, "21 miles needs 13 cones") Test.assertEquals(fizzbuzzy(99), 54, "99 miles needs 54 cones") Test.assertEquals(fizzbuzzy.toString().indexOf("if"), -1, "You made the goblin angry")
- Test.assertEquals(fizzbuzzy(21), 13, "21 miles needs 13 cones")
Test.assertEquals(fizzbuzzy(99), 54, "99 miles needs 54 cones")- Test.assertEquals(fizzbuzzy(99), 54, "99 miles needs 54 cones")
- Test.assertEquals(fizzbuzzy.toString().indexOf("if"), -1, "You made the goblin angry")
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.
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;
- }