Created
May 23, 2019 09:06
-
-
Save Kaeyz/4ca3d77da72aa6c3bdfe165795f53147 to your computer and use it in GitHub Desktop.
First Hacker rank challenge
This file contains 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
// Complete the calculateArea function below. | |
// It returns a Promise which on success, returns area of the shape, and on failure returns [-1]. | |
let calculateArea = (shape, values) => { | |
const myShape = { | |
rectangle: (l, b) => { return (l * b) }, | |
square: (l) => { return (l * l) }, | |
circle: (r) => { return (3.14 * r * r) }, | |
triangle: (b, h) => { return (b * h / 2) } | |
}; | |
return new Promise((resolve, reject) => { | |
if (Object.keys(myShape).includes(shape.toLowerCase())) { | |
switch (shape) { | |
case shape = "rectangle": | |
return resolve(myShape.rectangle(values[0], values[1])); | |
case shape = "square": | |
return resolve(myShape.square(values)); | |
case shape = "circle": | |
return resolve(myShape.circle(values)); | |
case shape = "triangle": | |
return resolve(myShape.triangle(values[0], values[1])); | |
default: | |
return reject([-1]); | |
} | |
} else { | |
return reject([-1]); | |
}; | |
}); | |
} | |
// Complete the generateArea function below. | |
// It returns a Promise which on success, returns an array of areas of all the shapes and on failure, returns [-1]. | |
let getAreas = async (shapes, values_arr) => { | |
let areas = []; | |
if (shapes.length === values_arr.length) { | |
for (let i = 0; i < shapes.length; i++) { | |
const area = await calculateArea(shapes[i], values_arr[i]) | |
Number.isInteger(area) ? areas.push(area) : areas.push(area.toFixed(1)); | |
}; | |
return areas; | |
} else { | |
return [-1]; | |
}; | |
}; | |
let shapes = ["square", "rectangle", "triangle", "circle"]; | |
let values_arr = [2, [3, 2], [2, 3], 5]; | |
getAreas(shapes, values_arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment