Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ten Zhi Yang
Ten Zhi Yang

Posted on • Originally published atblog.tenzhiyang.com

     

Paint can room coverage

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
Enter fullscreen modeExit fullscreen mode

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));
Enter fullscreen modeExit fullscreen mode

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));
Enter fullscreen modeExit fullscreen mode

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);};
Enter fullscreen modeExit fullscreen mode

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);};
Enter fullscreen modeExit fullscreen mode

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);};
Enter fullscreen modeExit fullscreen mode

This introduces a new bug in that the validation will pass if room is an empty object. We could check thatlengthwidth 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);};
Enter fullscreen modeExit fullscreen mode

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);};
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Some comments may only be visible to logged-in visitors.Sign in to view all comments.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Does weird things with javascript @ https://app.tenzhiyang.comemail: hi@tenzhiyang.com
  • Location
    Singapore
  • Work
    Senior Frontend Engineer
  • Joined

More fromTen Zhi Yang

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp