Last active
January 19, 2023 10:05
-
-
Save ozcanzaferayan/40871fe002408d7495b3c850a99c97f8 to your computer and use it in GitHub Desktop.
Gets percentage rounds in TypeScript
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
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | |
import { getPercentageRounds } from "./main.ts"; | |
Deno.test(function getPercentageRoundsTest() { | |
assertEquals(getPercentageRounds(0), "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪"); | |
assertEquals(getPercentageRounds(0.04), "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪"); | |
assertEquals(getPercentageRounds(0.1), "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪"); | |
assertEquals(getPercentageRounds(0.5), "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪"); | |
assertEquals(getPercentageRounds(0.6), "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪"); | |
assertEquals(getPercentageRounds(1), "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"); | |
}); |
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
export function getPercentageRounds(percentage: number) { | |
if (percentage < 0 || percentage > 1) { | |
throw new Error("Percentage must be between 0 and 1"); | |
} | |
const fullRounds = Math.ceil(percentage * 10); | |
const emptyRounds = 10 - fullRounds; | |
const fullRoundsString = "🔵".repeat(fullRounds); | |
const emptyRoundsString = "⚪".repeat(emptyRounds); | |
return `${fullRoundsString}${emptyRoundsString}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment