Binary Agent Issue

Binary Agent Issue
0

#1

Hi fellows,

I am on the binary agent challenge and I almost solved it but it doesn’t want to pass although the results are as expected. I don’t know what’s wrong . Here is my code

console.clear();
let letters = [];
function binaryAgent(str) {
  const dec = {
    0:128,
    1:64,
    2:32,
    3:16,
    4:8,
    5:4,
    6:2,
    7:1
  }
  let splStr = str.split(" ").map( item => {
    let word = item.split("");
    let num = 0;
    for (let j=0 ; j < word.length ; j++){
      if (word[j] == 1){
        num += dec[j]
      }
    }
    letters.push(String.fromCharCode(num));
  });
  return letters.join("");
}

console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
console.log("Aren't bonfires fun!?");


console.log(binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001"));
console.log("I love FreeCodeCamp!");


#2

It doesn’t like your let letters = []; being declared publicly, move the declaration inside your function and it will work


#3

It worked. Thanks bro :slight_smile: