Created
February 23, 2019 05:43
-
-
Save jdnichollsc/3ba81f4409f8031d39951dcd96f0900a to your computer and use it in GitHub Desktop.
Get areas of shapes - Hackerrank
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
const getArea = (shape, values) => { | |
let area = -1 | |
switch(shape) { | |
case "square": | |
area = Math.pow(values[0], 2); | |
break; | |
case "rectangle": | |
area = values[0] * values[1]; | |
break; | |
case "circle": | |
area = 3.14 * Math.pow(values[0], 2); | |
break; | |
case "triangle": | |
area = 0.5 * values[0] * values[1]; | |
break; | |
default: | |
break; | |
} | |
return area; | |
} | |
// Complete the calculateArea function below. | |
// It returns a Promise which on success, returns area of the shape, and on failure returns [-1]. | |
let calculateArea = async (shape, values) => { | |
const area = getArea(shape, values) | |
if (area == -1) { | |
throw new Error(-1) | |
} | |
else{ | |
return Math.floor(area * 100) / 100 | |
} | |
} | |
// 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) => { | |
try { | |
const areas = await Promise.all( | |
shapes.map((shape, index) => calculateArea(shape, values_arr[index])) | |
) | |
return areas; | |
} | |
catch { | |
return [-1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cok suwun