JavaScript functions Scenario: Your uncle wants to start a business building gar
ID: 3743552 • Letter: J
Question
JavaScript functions
Scenario: Your uncle wants to start a business building garden sheds in people's backyards. He needs a program to help calculate the materials list and cost for each shed. For the purposes of this assignment we are ignoring the roof and the foundation. We're only shopping for windows, doors, lumber, and plywood.
Task: Build a terminal-based node app that will ask questions about the dimensions of the shed, then output a shopping list and a cost. Assume the following about the sheds:
All sheds will have 8 foot tall walls
Stud spacing is 16" apart
Sheds can have between 0 and 3 windows (windows are 18" x 27")
Sheds will have one door, (doors are 72" x 80")
Shed walls can be between 6 and 20 feet long
Material cost (based on some Home Depot searching):
exterior grade plywood (Links to an external site.)Links to an external site. 4' x 8' sheet: $19.85 each
2"x4" 96in. kiln dried stud (Links to an external site.)Links to an external site.: $3.27 each
Fiberglass pre-hung door (Links to an external site.)Links to an external site., 72" x 80" - $631.28
Window with shutters (Links to an external site.)Links to an external site., 18" x 27" - $121.17
The program should output the following shopping list items for your uncle to buy:
The number of 4'x8' sheets of plywood needed
The number of 8' 2"x4" studs to buy
The number of windows needed
The number of doors needed (there's always only one)
Instructions:
First, start by installing the readline-sync (Links to an external site.)Links to an external site. node module
Next, ask questions about the dimensions of the shed
Use the readline-sync node module to prompt the user in the command line.
As the user the following questions:
What is the length of the shed?
What is the width of the shed?
How many windows will be installed?
Keep asking a question until a valid value is entered (hint: while loops are good for this!)
Write a function to compute the area of all shed walls combined
Determine the area of all the walls
Subtract the area of each of the windows and the door
This should give you the net sq. ft. of plywood you need, so determine how many sheets would be equal or greater than this (round up with Math.round() because you can't buy half-sheets of plywood)
Write a function to compute the number of studs needed for a single wall
Take in the length and width dimensions
Compute the number of 8' studs needed to span the width of each wall at the top and bottom (these are called plates and sills)
Divide the length of the wall by 16" to compute the number of 16" spans you will have
You will want to use the Math.round() function to round the spans to the nearest whole number, then add 1 - this will give you the number of studs needed for a wall.
Remember that there are four walls, so this function will need to be called multiple times to calculate all the studs needed
All that's left to do in the program is to call the functions and compute the numbers, then output the values
Use a template literal (Links to an external site.)Links to an external site. to output the values to the user with a console.log() statement
Explanation / Answer
//enable to prompt the user in the command line
let readlineSync = require('readline-sync');
//initzlize variables
let length_shed, width_shed, windows = -1;
let wall_area, plywood_amount, windowAndDoor_area, total_wall_area, number_studs, distance_stud;
let length_plywood, width_plywood, area_plywood;
//Each window size is 18" x 27" (1.5' x 2.25').
let width_window = 1.5;
let length_window = 2.25;
//calculate the area of the window
let window_area = width_window * length_window;
//Each door size is 72" x 80" (6' x 6.67' ).
let width_door = 6;
let length_door = 6.67;
//calculate the area of the door
let door_area = width_door * length_door;
/*
* ask the user info about the shed dimensions.
* The length and width of the shred need to be at least as same as the door.
* door : 72" x 80" (6' x 6.667')
* shed walls can be between 6 - 20 feet long
*/
while( length_shed < 6 || isNaN(length_shed) || length_shed > 20 ) {
length_shed = readlineSync.question('What is the length of the shed? ');
}
while( width_shed < 6 || isNaN(width_shed) || width_shed > 20 ) {
width_shed = readlineSync.question('What is the width of the shed? ');
}
//sheds can have between 0 and 3 windows
while( windows < 0 || isNaN(windows) || windows > 3) {
windows = readlineSync.question('How many windows will be installed? ');
}
/*
* Calculate the area of all walls combined and subtract out the area of the door and windows.
* Shed is 8' tall, and we need 2 of each wall
*/
let length_wall = 8;
let number_walls_needed = 2;
total_wall_area =((length_shed * length_wall) * number_walls_needed) + ((width_shed * length_wall) * number_walls_needed);
windowAndDoor_area = parseFloat(Math.round(window_area * windows + door_area).toFixed(2));
wall_area = parseFloat(Math.round(total_wall_area - windowAndDoor_area).toFixed(2));
// Calculates the amount of plywood sheets that will be need to be purchased. Plywood sheets are 4' x 8'.
// Math.ceil forces number to round up since plywood must be purchased whole.
length_plywood = 8;
width_plywood = 4;
area_plywood = length_plywood * width_plywood;
plywood_amount = wall_area / area_plywood;
/*
* Print to the terminal what are need to buy
*/
console.log("You will need to purchase " + Math.ceil(plywood_amount) +" plywood sheets.");
console.log('You will need to purchase 1 door.');
console.log('You will need to purchase ' + windows + ' windows.');
/*
* calculate the number fo studs needed, each stud is 16"(1.33') apart
* add 1 to the number of studs at the end
*/
distance_stud = 1.33;
number_studs = Math.round((length_shed/distance_stud + width_shed/distance_stud) * 2) + 1;
console.log("You will need " + number_studs + " studs.");
/*
* exit the program
*/
process.exit(0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.