Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2019
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function carFactory(obj) {
  2.  
  3.     function createEngine(hp) {
  4.         const engine = {}
  5.         if (hp <= 90) {
  6.             engine.power = 90;
  7.             engine.volume = 1800;
  8.         } else if (hp <= 120) {
  9.             engine.power = 120;
  10.             engine.volume = 2400;
  11.         } else if (hp <= 200) {
  12.             engine.power = 200;
  13.             engine.volume = 3500;
  14.         }
  15.         return engine;
  16.     }
  17.  
  18.     function createCarriage(color, type) {
  19.         const carriage = { color, type };
  20.         return carriage;
  21.     }
  22.  
  23.     function makeWheels(wheelSize) {
  24.         let newSize = wheelSize % 2 == 0 ? wheelSize - 1 : wheelSize;
  25.         const wheels = new Array(4).fill(newSize);
  26.         return wheels;
  27.     }
  28.     return {
  29.         model: obj.model,
  30.         engine: createEngine(obj.power),
  31.         carriage: createCarriage(obj.color, obj.carriage),
  32.         wheelsize: makeWheels(obj.wheelsize)
  33.     }
  34. }
  35.  
  36. //tests
  37. console.log(carFactory({
  38.     model: 'Opel Vectra',
  39.     power: 110,
  40.     color: 'grey',
  41.     carriage: 'coupe',
  42.     wheelsize: 17
  43. }));
  44.  
  45. console.log(carFactory({
  46.     model: 'VW Golf II',
  47.     power: 90,
  48.     color: 'blue',
  49.     carriage: 'hatchback',
  50.     wheelsize: 14
  51. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement