// ------------------------------------------------------------
// digitsToNumberJoin
// Converts an array of digits into a number by joining them.
// Example: [1,2,3,4] → "1234" → 1234
// ------------------------------------------------------------
function digitsToNumberJoin(digits: number[]): number {
return Number(digits.join(""));
}
// ------------------------------------------------------------
// digitsToNumberMath
// Pure mathematical folding (no string operations).
// Example: [1,2,3,4] → (((1*10)+2)*10+3)*10+4
// ------------------------------------------------------------
function digitsToNumberMath(digits: number[]): number {
let n: number = 0;
for (const d: number of digits) {
n = n * 10 + d;
}
return n;
}
// ------------------------------------------------------------
// Main
// ------------------------------------------------------------
const digits: number[] = [4, 6, 3, 9, 1, 2];
console.log("Using join():", digitsToNumberJoin(digits));
console.log("Using math():", digitsToNumberMath(digits));
/*
run:
Using join(): 463912
Using math(): 463912
*/