Created
November 28, 2018 04:28
-
-
Save ltackett/496e743ae6802bf0b4cf5484c6d3d414 to your computer and use it in GitHub Desktop.
Car Factory
This file contains hidden or 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
class Car { | |
constructor(length, doors) { | |
try { | |
if (doors < 1) | |
throw 'We cannot enter a doorless car.' | |
if (length < 7) | |
throw 'We cannot build that small car.' | |
if (length / doors < 1 || (length-3) < (doors*2)) | |
throw 'We cannot fit that many doors in that car.' | |
} catch(e) { | |
new Exception(e) | |
} | |
this.length = length | |
this.doors = doors | |
} | |
makeRoof() { | |
return ` ${this.parts.roof.repeat(this.length - 3)}` | |
} | |
makeDoors() { | |
const rearDoors = new Array(this.doors >= 2 ? Math.floor(this.doors/2) : 0).fill(this.parts.door) | |
const frontDoors = new Array(this.doors >= 2 ? Math.ceil(this.doors/2) : 0).fill(this.parts.door) | |
const emptySpaces = ' '.repeat((this.length - 3) - (this.doors * 2)) | |
if (this.doors === 1) { | |
frontDoors.push(this.parts.door) | |
} | |
return `|${rearDoors.join('')}${emptySpaces}${frontDoors.join('')}\\` | |
} | |
makeChassis() { | |
let axles = 2 | |
if (this.length >= 12) { | |
this.length % 2 | |
? axles = (this.length - 11)/2 + 2 | |
: axles = (this.length - 12)/2 + 3 | |
} | |
const rearAxles = new Array(axles >= 2 ? Math.ceil(axles/2) : 0).fill(this.parts.axle) | |
const frontAxles = new Array(axles >= 2 ? Math.floor(axles/2) : 0).fill(this.parts.axle) | |
const emptySpaces = '-'.repeat((this.length - 2) - (Math.ceil(axles) * 2)) | |
return `-${rearAxles.join('')}${emptySpaces}${frontAxles.join('')}'` | |
} | |
get parts() { | |
return { | |
roof: '_', | |
door: '[]', | |
axle: 'o-', | |
} | |
} | |
get body() { | |
return { | |
component: `${this.makeRoof()}\n${this.makeDoors()}\n` | |
} | |
} | |
get chassis() { | |
return { | |
component: this.makeChassis() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment