forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexToDecimal.js
More file actions
Latest commit
23 lines (21 loc) · 623 Bytes
/
Copy pathHexToDecimal.js
File metadata and controls
23 lines (21 loc) · 623 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
functionhexToInt(hexNum){
constnumArr=hexNum.split('')// converts number to array
returnnumArr.map((item,index)=>{
switch(item){
case'A': return10
case'B': return11
case'C': return12
case'D': return13
case'E': return14
case'F': return15
default: returnparseInt(item)
}
})
}
functionhexToDecimal(hexNum){
constintItemsArr=hexToInt(hexNum)
returnintItemsArr.reduce((accumulator,current,index)=>{
returnaccumulator+(current*Math.pow(16,(intItemsArr.length-(1+index))))
},0)
}
export{hexToInt,hexToDecimal}