Skip to content

Instantly share code, notes, and snippets.

@sirianni
Created June 18, 2019 14:05
Show Gist options
  • Save sirianni/5edf6d195fe20891a2ac0bff595a2f3f to your computer and use it in GitHub Desktop.
Save sirianni/5edf6d195fe20891a2ac0bff595a2f3f to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wosewoh
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script id="jsbin-javascript">
// SETTING UP TEST VARIABLES DO NOT EDIT
var refrigerators = [
{ id: 1, temp: 40, pluggedIn: false, milk: 2 },
{ id: 2, temp: 32, pluggedIn: true, milk: 1, butter: 2 },
{ id: 3, temp: 75, pluggedIn: true, milk: 1 },
{ id: 4, temp: 35, pluggedIn: true, eggs: 12, cheese: 2 },
{ id: 6, temp: 36, pluggedIn: true, milk: 1 },
{ id: 5, temp: 24, pluggedIn: true, milk: 1 },
];
// MAKE CHANGES HERE TO THESE TWO FUNCTIONS
function spoiledMilk(fridges) {
var sum = 0;
$.each(refrigerators, function(i, fridge) {
if (fridge.temp > 36) {
sum + fridge.milk;
}
});
return sum;
}
function needsRepair(fridges) {
var report = [];
for(var i = 0; i <= fridges.length; i++) {
if (fridges[i].pluggedIn) {
report.push(i);
}
}
return report;
}
// TESTING PROGRAM DO NOT EDIT
console.log('Spoiled milk should be 3 == ' + spoiledMilk(refrigerators));
console.log('Needs repair should be ids [1] == [' + needsRepair(refrigerators) + ']');
var newFridges = [
{ id: 9, temp: 68, pluggedIn: false, milk: 14},
{ id: 10, temp: 33, pluggedIn: false, milk: 1},
{ id: 13, temp: 35, pluggedIn: true, milk: 1},
{ id: 14, temp: 50, pluggedIn: true},
{ id: 16, temp: 37, pluggedIn: false, milk: 1},
]
console.log('Spoiled milk should be 15 == ' + spoiledMilk(newFridges));
console.log('Needs repair should be ids [9,10,16] == [' + needsRepair(newFridges) + ']');
</script>
<script id="jsbin-source-javascript" type="text/javascript">// SETTING UP TEST VARIABLES DO NOT EDIT
var refrigerators = [
{ id: 1, temp: 40, pluggedIn: false, milk: 2 },
{ id: 2, temp: 32, pluggedIn: true, milk: 1, butter: 2 },
{ id: 3, temp: 75, pluggedIn: true, milk: 1 },
{ id: 4, temp: 35, pluggedIn: true, eggs: 12, cheese: 2 },
{ id: 6, temp: 36, pluggedIn: true, milk: 1 },
{ id: 5, temp: 24, pluggedIn: true, milk: 1 },
];
// MAKE CHANGES HERE TO THESE TWO FUNCTIONS
function spoiledMilk(fridges) {
var sum = 0;
$.each(refrigerators, function(i, fridge) {
if (fridge.temp > 36) {
sum + fridge.milk;
}
});
return sum;
}
function needsRepair(fridges) {
var report = [];
for(var i = 0; i <= fridges.length; i++) {
if (fridges[i].pluggedIn) {
report.push(i);
}
}
return report;
}
// TESTING PROGRAM DO NOT EDIT
console.log('Spoiled milk should be 3 == ' + spoiledMilk(refrigerators));
console.log('Needs repair should be ids [1] == [' + needsRepair(refrigerators) + ']');
var newFridges = [
{ id: 9, temp: 68, pluggedIn: false, milk: 14},
{ id: 10, temp: 33, pluggedIn: false, milk: 1},
{ id: 13, temp: 35, pluggedIn: true, milk: 1},
{ id: 14, temp: 50, pluggedIn: true},
{ id: 16, temp: 37, pluggedIn: false, milk: 1},
]
console.log('Spoiled milk should be 15 == ' + spoiledMilk(newFridges));
console.log('Needs repair should be ids [9,10,16] == [' + needsRepair(newFridges) + ']');</script></body>
</html>
// SETTING UP TEST VARIABLES DO NOT EDIT
var refrigerators = [
{ id: 1, temp: 40, pluggedIn: false, milk: 2 },
{ id: 2, temp: 32, pluggedIn: true, milk: 1, butter: 2 },
{ id: 3, temp: 75, pluggedIn: true, milk: 1 },
{ id: 4, temp: 35, pluggedIn: true, eggs: 12, cheese: 2 },
{ id: 6, temp: 36, pluggedIn: true, milk: 1 },
{ id: 5, temp: 24, pluggedIn: true, milk: 1 },
];
// MAKE CHANGES HERE TO THESE TWO FUNCTIONS
function spoiledMilk(fridges) {
var sum = 0;
$.each(refrigerators, function(i, fridge) {
if (fridge.temp > 36) {
sum + fridge.milk;
}
});
return sum;
}
function needsRepair(fridges) {
var report = [];
for(var i = 0; i <= fridges.length; i++) {
if (fridges[i].pluggedIn) {
report.push(i);
}
}
return report;
}
// TESTING PROGRAM DO NOT EDIT
console.log('Spoiled milk should be 3 == ' + spoiledMilk(refrigerators));
console.log('Needs repair should be ids [1] == [' + needsRepair(refrigerators) + ']');
var newFridges = [
{ id: 9, temp: 68, pluggedIn: false, milk: 14},
{ id: 10, temp: 33, pluggedIn: false, milk: 1},
{ id: 13, temp: 35, pluggedIn: true, milk: 1},
{ id: 14, temp: 50, pluggedIn: true},
{ id: 16, temp: 37, pluggedIn: false, milk: 1},
]
console.log('Spoiled milk should be 15 == ' + spoiledMilk(newFridges));
console.log('Needs repair should be ids [9,10,16] == [' + needsRepair(newFridges) + ']');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment