mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Added Oct to Decimal conversion
This commit is contained in:
15
Conversions/OctToDecimal.js
Normal file
15
Conversions/OctToDecimal.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
function octalToDecimal (num) {
|
||||||
|
let dec = 0
|
||||||
|
let base = 1
|
||||||
|
while (num > 0) {
|
||||||
|
const r = num % 10
|
||||||
|
num = Math.floor(num / 10)
|
||||||
|
dec = dec + (r * base)
|
||||||
|
base = base * 8
|
||||||
|
}
|
||||||
|
return dec
|
||||||
|
}
|
||||||
|
|
||||||
|
// test cases
|
||||||
|
console.log(octalToDecimal(56) === 46)
|
||||||
|
console.log(octalToDecimal(2365) === 1269)
|
Reference in New Issue
Block a user