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
32 lines (30 loc) · 687 Bytes
/
Copy pathHexToDecimal.js
File metadata and controls
32 lines (30 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)=>{
return(
accumulator+current*Math.pow(16,intItemsArr.length-(1+index))
)
},0)
}
export{hexToInt,hexToDecimal}