Having Trouble With Binary Agents Challenge

Tell us what’s happening:
So far in this challenge, I am trying to convert the binary sequence into decimal (I am not yet finished with it) but I am hitting an error.

  **Your code so far**

function binaryAgent(str) {
var split = str.split(" ");
for (var i = 0; i < split.length; i++) {
  var arr2 = [];
  var sum = 0;
  var reverse = split[i].split("").reverse().join("");
// Below is the part where I'm stuck at
  for (var j = 0; j < reverse.length; j++) {
    if (reverse[j] === 1) {
      arr2.push(Math.pow(2, j));
    }
   }
  }
}

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

I tried to console.log() the Math.pow(2, j), and it didn’t return anything, which really confused me. When I removed the if statement and tested out console.log(reverse[j] === 1) EVERYTHING was false, even the ones! Why is this? If anybody could explain to me what is going on I would really, really appreciate it!

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Binary Agents

Link to the challenge:

you are doing

"1" === 1
"0" === 1

they are both false

1 Like

In your condition, you are use the number 1 but reverse is a string so reverse[j] will always be a string. "1" === 1 is false.

1 Like

Oh, it’s a string! Maybe I should change the if statement to:

if (reverse[j] === “1”)

OR

if (reverse[j] == 1)

Thanks so much!

I solved it!! Thanks!

Congratulations! This isn’t any easy challenge to work through.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.