Ethiopian multiplication: Difference between revisions

→‎{{header|TypeScript}}: Standard functions are applied to format numbers.
(→‎{{header|TypeScript}}: Standard functions are applied to format numbers.)
Line 5,688:
<lang javascript>
// Ethiopian multiplication
 
function intToString(n: number, wdth: number): string {
sn = Math.floor(n).toString();
len = sn.length;
return (wdth < len ? "#".repeat(wdth) : " ".repeat(wdth - len) + sn);
}
 
function double(a: number): number {
return 2 * a;
}
 
function halve(a: number): number {
return Math.floor(a / 2);
}
 
function isEven(a: number): bool {
return a % 2 == 0;
}
 
function showEthiopianMultiplication(x: number, y: number): void {
var tot = 0;
while (x >= 1) {
process.stdout.write(intToString(x.toString().padStart(9, 9' ') + " ");
if (!isEven(x)) {
tot += y;
process.stdout.write(intToString(y.toString().padStart(9, 9' '));
}
console.log();
Line 5,719 ⟶ 5,713:
y = double(y);
}
console.log("=" + " ".repeat(9) + intToString(tot.toString().padStart(9, 9' '));
}
 
showEthiopianMultiplication(17, 34);
</lang>
Anonymous user