Advertisement
PowerCell46

Binary to decimal JS

Nov 22nd, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function binaryToDecimal(binary) {
  2.  
  3. let finalResult = 0;
  4. let power = -1;
  5.  
  6. for (let binaryIndex = (Number(binary.length) - 1); binaryIndex >= 0; binaryIndex--) {
  7.  
  8. let currentDigit = Number(binary[binaryIndex]);
  9. power++;
  10.  
  11. let currentResult = currentDigit * (Math.pow(2, power));
  12. finalResult += currentResult;
  13. }
  14.  
  15. console.log(finalResult);
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement