diff --git a/codewars/7kyu/width-height-ratio/koronya.js b/codewars/7kyu/width-height-ratio/koronya.js new file mode 100644 index 000000000..f2eee8d22 --- /dev/null +++ b/codewars/7kyu/width-height-ratio/koronya.js @@ -0,0 +1,22 @@ +// [JS][7kyu] Width-Height Ratio +// width-height-ratio +// https://www.codewars.com/kata/55486cb94c9d3251560000ff/train/javascript + +const getGcd = (a, b) => { + if (a === 0) { + return b + } + return getGcd(b % a, a) +} + +const calculateRatio = (w, h) => { + if (w === 0 || h === 0) { + throw new Error('Width and height must be greater than 0') + } + + const divisor = getGcd(w, h) + return `${w / divisor}:${h / divisor}` +} + +const results1 = calculateRatio(1024, 768) +const results2 = calculateRatio(1920, 1080) \ No newline at end of file