This may or may not be the optimal solution, DO NOT blindly memorize my solutions
FromCassidy's weeklynewsletter.
Target audience: beginners in js (please feedback!)
This week’s question:
Given a room size, and the square footage a paint can can cover, return how many cans of paint you need to buy to paint a room. Assume the room has four walls. If you’d like to expand this, you can add doors, windows, or any other room features that might make the problem interesting to solve.
Example:
room = { length: 12, width: 10, height: 9 }canCoverage = 200$ numberOfCans(room, canCoverage)$ 2 // (12x9x2)+(10x9x2) = 396, so two cans will cover it
This question is logically fairly simple, in fact most of the solution is already given in the example!
constcalcCanCoverage=(length,width,height,canCoverage)=>Math.ceil((length*height*2+width*height*2)/canCoverage);console.log(calcCanCoverage(12,10,9,200));
Now the input isn't really in that format, so let's find some way to extract that information.
constroom={length:12,width:10,height:9};constcanCoverage=200;constcalcCanCoverage=(length,width,height,canCoverage)=>Math.ceil((length*height*2+width*height*2)/canCoverage);constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;returncalcCanCoverage(length,width,height,canCoverage);};console.log(numberOfCans(room,canCoverage));
And that's the whole thing solved! However, since this is not much of an article, let's attempt some input management. One of the things we want to handle is if room is not an object that has our length width or height. Here we can think of using some value to show "error". Logically thinking, we can use either-1
ornull
as the error response. In this case, I'll use -1.
constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;if(!(length&&width&&height)){return-1;}returncalcCanCoverage(length,width,height,canCoverage);};
This will handle cases where length, width and height are 0, any one of those isNaN
(if room is not an object) and also if any one of those isnull
orundefined
(if room is empty object).
We should also handle if length, width and height is a number. let's type-cast in our input validation
constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;if(!(Number(length)&&Number(width)&&Number(height))){return-1;}returncalcCanCoverage(length,width,height,canCoverage);};
This will allow'12'
to be type-casted into a number,'12a'
will be NaN. Booleans will be converted into 1 or 0.
If we want to add another check right now, it would be quite verbose to check everything again, so let's useObject.values
on the room object, so that we get all the values, and anevery
function to test every integer.
constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;if(!Object.values(room).every((val)=>Number(val))){return-1;}returncalcCanCoverage(length,width,height,canCoverage);};
This introduces a new bug in that the validation will pass if room is an empty object. We could check thatlength
width
andheight
exists in the object, but I would just check if room is an object and has values.
constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;if(!room||Object.keys(room).length===0||!Object.values(room).every((val)=>Number(val))){return-1;}returncalcCanCoverage(length,width,height,canCoverage);};
Now the last thing I can think of is to check if the room dimensions are positive, so let's add that in theevery
function
constnumberOfCans=(room,canCoverage)=>{const{length,width,height}=room;if(!room||Object.keys(room).length===0||!Object.values(room).every((val)=>Number(val)>0)){return-1;}returncalcCanCoverage(length,width,height,canCoverage);};
Top comments(0)
Some comments may only be visible to logged-in visitors.Sign in to view all comments.
For further actions, you may consider blocking this person and/orreporting abuse