Created
September 12, 2022 10:02
-
-
Save ambrizals/8cce40196b5cf30e5e4b2562cc75e1a9 to your computer and use it in GitHub Desktop.
Nyoba buat formula di 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 { run } from "formula"; | |
enum RumusType { | |
OPERATOR, | |
VARIABLE, | |
FIXEDVALUE, | |
} | |
interface Rumus { | |
urutan: number; | |
exp: string; | |
description: string; | |
type: RumusType; | |
} | |
const rumus: Rumus[] = [ | |
{ | |
urutan: 1, | |
exp: "keliling", | |
description: "Ukuran keliling benda", | |
type: RumusType.VARIABLE, | |
}, | |
{ | |
urutan: 2, | |
exp: "*", | |
description: "Kali", | |
type: RumusType.OPERATOR, | |
}, | |
{ | |
urutan: 3, | |
exp: "1500000", | |
description: "Nilai harga per meter keliling", | |
type: RumusType.FIXEDVALUE, | |
}, | |
]; | |
const injectFormula = (rumus: Rumus[]) => { | |
let formula: string = ""; | |
for (const rms of rumus) { | |
formula = formula + rms.exp; | |
} | |
return formula; | |
}; | |
const result = run(injectFormula(rumus), { keliling: 2 }); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment