Binary to integer translator

I’m writing a function that takes an array of boolean values (specifically 1’s and 0’s) and changes each number into its base10 equivalent integer and then adds them together. The problem that I’m having is that instead of checking whether each particular element is a one or a zero and then inserting the base10 number in its place, the function is making each element a 1 and THEN performs the integer switch and the sum. So my return command at the end always results in a value of 15.

     arr = ([8, 4, 2, 1])
     for elem in arr:
         if arr[0] == 1:
             arr[0] = 8
         elif arr[0] == 0:
             arr[0] = 0
         if arr[1] == 1:
             arr[1] = 4
         elif arr[1] == 0:
             arr[1] = 0
         if arr[2] == 1:
             arr[2] = 2
         elif arr[2] == 0:
             arr[2] = 0
         if arr[3] == 1:
             arr[3] = 1
         elif arr[3] == 0:
             arr[3] = 0
     
         return sum(arr)```
I wrote this with the idea that the function would check each element and if the element is a 1 then the appropriate integer would be instantiated in its place and if the element was a 0 then the element would remain a 0, but what's happening is that each element is being changed to a 1 and then being checked by the function.

It looks like you have hardcoded logic based on array position, so why do you have a for loop?

I wanted to see what would happen if I added the for loop. Maybe an error that came up would help figure out what else could be going wrong but the for loop didn’t change my output at all so I’ve removed it.

      arr = ([8, 4, 2, 1])
      if arr[0] == 1:
          arr[0] = 8
      elif arr[0] == 0:
          arr[0] = 0
      if arr[1] == 1:
          arr[1] = 4
      elif arr[1] == 0:
          arr[1] = 0
      if arr[2] == 1:
          arr[2] = 2
      elif arr[2] == 0:
          arr[2] = 0
      if arr[3] == 1:
          arr[3] = 1
      elif arr[3] == 0:
          arr[3] = 0
      
      return sum(arr)```
This is my current logic. Sill returns a value of 15.
UPDATE
I just figured out what I did wrong. I removed the ```arr = (8, 4, 2, 1)``` and it worked. 
That line was changing the input array and that's why I kept getting a ```return``` of 15.

Well, you’re not changing the values of arr at all.
arr[0] is 8, which you don’t have a case for.
arr[1] is 4, which you don’t have a case for.
arr[2] is 2, which you don’t have a case for.
arr[3] is 1, which doesn’t change.

this function is designed to accept a 4 digit input array and translate the sum of its elements into a base10 integer but my first line was changing the input array (whatever it might be) to always be (8, 4, 2, 1) which always adds to 15. So I had designed a function that will only return a value of 15. removing the first line allows for the function to work as intended, accepting a 4 digit input and doing the math for me.

That looks a little confusing to follow because it is hard coded in. I think you could possibly reverse the array of 1s and 0s and then take 2^(position in the array + 1) for all 1s and then add that to a total and return that.

I don’t think I understand what you’re suggesting and I definitely have no idea how to implement it.

Binary / Decimal conversion is the very low-level core of computing and every language obviously has to have implementation of it:

let bin = '100100011101010111100';
let dec = parseInt(bin, 2); // 1194684

// and back
let d = dec.toString(10); // '1194684'
let b = dec.toString(2); // '100100011101010111100'
let o = dec.toString(8); // '4435274'
let h = dec.toString(16); // '123abc'

// and back again using + / Number conversion
let nd = +(d); // 1194684
let nb = +('0b' + b); // 1194684
let no = +('0o' + o); // 1194684
let nh = +('0x' + h); // 1194684