Skip to content

Instantly share code, notes, and snippets.

@sharkySharks
Created December 22, 2014 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharkySharks/c6c5509ae68f92078509 to your computer and use it in GitHub Desktop.
Save sharkySharks/c6c5509ae68f92078509 to your computer and use it in GitHub Desktop.
/*
MakerSquare Coding Challenge #2
By Kayden Althen
Cookie/ingredient program that allows users to add and subtract values for sugar
and flour ingredients as well as cookies requested and money spent on ingredients.
*/
$(document).ready(function() {
//Inventory kept in .js file to prevent cookie counterfeiting
var inventory = {
product: {
money: 1000,
cookies: 0
},
ingredients: {
sugar: 10,
flour: 10
},
pot: {
sugar: 0,
flour: 0
}
};
//Using sugar decrements ingredient by 1, increases inventory.pot.sugar by 1
//alerts when user wants to use sugar after value hits '0'
$("#ingredients").find(".use-sugar").click(function () {
if (inventory.ingredients.sugar > 0) {
inventory.ingredients.sugar--;
$("#ingredients").find(".sugar").html(inventory.ingredients.sugar);
inventory.pot.sugar++;
$("#pot").find(".sugar").html(inventory.pot.sugar);
} else {
alert("Time to buy more sugar!");
}
});
//Using flour decrements ingredient by 1, increases inventory.pot.flour by 1
//alerts when user wants to use flour after value hits '0'
$("#ingredients").find(".use-flour").click(function () {
if (inventory.ingredients.flour > 0) {
inventory.ingredients.flour--;
$("#ingredients").find(".flour").html(inventory.ingredients.flour);
inventory.pot.flour++;
$("#pot").find(".flour").html(inventory.pot.flour);
} else {
alert("Time to buy more flour!");
}
});
//Requests for making cookies, logic looks at ingredients available if not already in the pot
$("#pot").find("button").click(function () {
if (inventory.pot.flour >= 6 && inventory.pot.sugar >= 3) {
inventory.pot.flour-=6;
$("#pot").find(".flour").html(inventory.pot.flour);
inventory.pot.sugar-=3;
$("#pot").find(".sugar").html(inventory.pot.sugar);
inventory.product.cookies++;
$("#product").find(".cookies").html(inventory.product.cookies);
} else if (inventory.pot.flour < 6) {
if (inventory.pot.flour + inventory.ingredients.flour > 5) {
alert("Please add flour from the ingredients first.");
} else {
alert("We are out of ingredients and unable to make your cookie at this time.\nSo sorry, please try again later.\n\nTotal ingredients in stock:\nFlour: " + (inventory.ingredients.flour + inventory.pot.flour) + "\nSugar: " + (inventory.ingredients.sugar + inventory.pot.sugar));
}
} else if (inventory.pot.sugar < 3) {
if (inventory.pot.sugar + inventory.ingredients.sugar > 2) {
alert("Please add sugar from the ingredients first.");
} else {
alert("We are out of ingredients and unable to make your cookie at this time.\nSo sorry, please try again later.\n\nTotal ingredients in stock:\nFlour: " + (inventory.ingredients.flour + inventory.pot.flour) + "\nSugar: " + (inventory.ingredients.sugar + inventory.pot.sugar));
}
}
});
//Buying sugar decrements inventory.product.money by 10, increases inventory.ingredients.sugar by 1
//does not allow the user to go beyond $0.00
$("#ingredients").find(".buy-sugar").click(function () {
if (inventory.product.money > 0) {
inventory.ingredients.sugar++;
$("#ingredients").find(".sugar").html(inventory.ingredients.sugar);
inventory.product.money-=10;
$("#product").find(".money").html(inventory.product.money);
} else {
alert("No more money to spend right now.");
}
});
//Buying flour decrements inventory.product.money by 20, increases inventory.ingredients.flour by 1
//does not allow the user to go beyond $0.00
$("#ingredients").find(".buy-flour").click(function () {
if (inventory.product.money > 0) {
inventory.ingredients.flour++;
$("#ingredients").find(".flour").html(inventory.ingredients.flour);
inventory.product.money-=20;
$("#product").find(".money").html(inventory.product.money);
} else {
alert("No more money to spend right now.");
}
});
});
/*
/*
MakerSquare Coding Challenge 2
By Kayden Althen
Algorithms Challenge: Give an array of integers, find the smallest difference between any two elements of the array.
*/
var arrayInt = [700,654,32,-400,55,999];
var findSmallestDifference = function(arr) {
//sort array
arr.sort(function(a,b) {
return a-b
});
//loop through sorted array to get the difference between array element values
//push those values to a new array and then loop through the new array to find the lowest value
var diff=[];
var lowest=0;
for (var b = 0; b < (arr.length - 1); b++) {
diff.push(arr[b+1] - arr[b]);
}
for (var c = 0; c < diff.length; c++) {
if (diff[c] < diff[lowest]) {
lowest = c;
}
}
//the lowest value index in the new array is the same index as one of the parameter array's smallest
//difference elements, the other is the next index in the array.
var result = [arr[lowest], arr[lowest + 1]];
return result;
};
findSmallestDifference(arrayInt);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment