Last active
July 13, 2023 19:00
-
-
Save crhallberg/b455100fc73790a04d257d6eb5982f21 to your computer and use it in GitHub Desktop.
Javascript - Working with Files - Starting Point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set some useful values at the top (Constants) | |
let MULCH = 2; | |
let INCHES_IN_A_YARD = 36 * 36 * 36; | |
// Make Variables to add up all of the yards. | |
let mulchTotal = 0; | |
let compostTotal = 0; | |
// Create a Function to calculate the yards of compost and mulch. | |
// This Function allows us to reuse the code within whenever we want. | |
// When we "pass" a garden in, it's saved as this variable (garden) | |
function doCalculations(garden) { | |
// We want 2 inches of mulch | |
// This updates the Object in the List | |
garden.mulchDepth = MULCH; | |
garden.compostDepth = garden.fillDepth - MULCH; | |
// UNLESS we have 2 inches or less | |
if (garden.fillDepth <= MULCH) { | |
garden.mulchDepth = 1; | |
garden.compostDepth = garden.fillDepth - 1; | |
} | |
// The actual volume math: width * length * depth; | |
garden.mulchYards = | |
(garden.width * garden.length * garden.mulchDepth) / INCHES_IN_A_YARD; | |
garden.compostYards = | |
(garden.width * garden.length * garden.compostDepth) / INCHES_IN_A_YARD; | |
// Print it out all fancy | |
console.log(garden.name); | |
console.log("Mulch Volume", garden.mulchYards); | |
console.log("Compost Volume", garden.compostYards); | |
console.log(); | |
mulchTotal = mulchTotal + garden.mulchYards; | |
compostTotal = compostTotal + garden.compostYards; | |
} | |
// Create all the data for two garden boxes | |
// Back Wall – Beans 19 32 2 | |
let beanBed = { | |
name: "Bean Bed", | |
width: 19, // inches | |
length: 32, // inches | |
fillDepth: 2, // inches | |
}; | |
// Box 1 – Tomatoes 57 137 5 | |
let tomatoBox = { | |
name: "Tomato Box", | |
width: 57, // inches | |
length: 137, // inches | |
fillDepth: 5, // inches | |
}; | |
// Put all of those gardens into a List. | |
// Lists allow us to do mass operations on large groups of values. | |
let gardenList = [ | |
{ | |
name: "Back Wall – Beans", | |
width: 19, | |
length: 32, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Box 1 – Tomatoes", | |
width: 57, | |
length: 137, | |
fillDepth: 5, | |
mulchDepth: 2, | |
}, | |
{ | |
name: "Box 2A – Birds", | |
width: 57, | |
length: 70, | |
fillDepth: 7, | |
mulchDepth: 2, | |
}, | |
{ | |
name: "Box 2B – Herbs", | |
width: 57, | |
length: 62, | |
fillDepth: 5, | |
mulchDepth: 2, | |
}, | |
{ | |
name: "Fountain Bed", | |
width: 24, | |
length: 42, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Hosta Bed", | |
width: 12, | |
length: 24, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Inside Fence – Right", | |
width: 48, | |
length: 32, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Neighbor Fence", | |
width: 60, | |
length: 72, | |
fillDepth: 4, | |
mulchDepth: 2, | |
}, | |
{ | |
name: "Outside Fence – Left", | |
width: 42, | |
length: 185, | |
fillDepth: 3, | |
mulchDepth: 2, | |
}, | |
{ | |
name: "Outside Fence – Right", | |
width: 52, | |
length: 178, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Porch – Left", | |
width: 30, | |
length: 108, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Porch – Side / Mixing", | |
width: 84, | |
length: 30, | |
fillDepth: 2, | |
mulchDepth: 1, | |
}, | |
{ | |
name: "Under Window", | |
width: 32, | |
length: 81, | |
fillDepth: 3, | |
mulchDepth: 2, | |
}, | |
]; | |
// Use .forEach on the List to send each garden to the Function above. | |
// "Call doCalculations on each garden inside of gardenList". | |
gardenList.forEach(doCalculations); | |
// Print out the totals (added up inside of doCalculations). | |
console.log(mulchTotal); | |
console.log(compostTotal); | |
function formatForExcel(garden) { | |
console.log(garden.compostDepth + "\t" + garden.mulchYards + "\t" + garden.compostDepth); | |
} | |
gardenList.forEach(formatForExcel); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Back Wall – Beans", | |
"width": 19, | |
"length": 32, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Box 1 – Tomatoes", | |
"width": 57, | |
"length": 137, | |
"fillDepth": 5, | |
"mulchDepth": 2 | |
}, | |
{ | |
"name": "Box 2A – Birds", | |
"width": 57, | |
"length": 70, | |
"fillDepth": 7, | |
"mulchDepth": 2 | |
}, | |
{ | |
"name": "Box 2B – Herbs", | |
"width": 57, | |
"length": 62, | |
"fillDepth": 5, | |
"mulchDepth": 2 | |
}, | |
{ | |
"name": "Fountain Bed", | |
"width": 24, | |
"length": 42, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Hosta Bed", | |
"width": 12, | |
"length": 24, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Inside Fence – Right", | |
"width": 48, | |
"length": 32, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Neighbor Fence", | |
"width": 60, | |
"length": 72, | |
"fillDepth": 4, | |
"mulchDepth": 2 | |
}, | |
{ | |
"name": "Outside Fence – Left", | |
"width": 42, | |
"length": 185, | |
"fillDepth": 3, | |
"mulchDepth": 2 | |
}, | |
{ | |
"name": "Outside Fence – Right", | |
"width": 52, | |
"length": 178, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Porch – Left", | |
"width": 30, | |
"length": 108, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Porch – Side / Mixing", | |
"width": 84, | |
"length": 30, | |
"fillDepth": 2, | |
"mulchDepth": 1 | |
}, | |
{ | |
"name": "Under Window", | |
"width": 32, | |
"length": 81, | |
"fillDepth": 3, | |
"mulchDepth": 2 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment