From 5c57453145ab8b3e1954fb2f8d3481cb3e15ae4c Mon Sep 17 00:00:00 2001 From: koronya Date: Fri, 13 Mar 2026 03:50:34 +0900 Subject: [PATCH] [JS][7kyu] Width-Height Ratio --- codewars/7kyu/width-height-ratio/koronya.js | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 codewars/7kyu/width-height-ratio/koronya.js 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