Still need more help: My decoding binary numbers into english letters works, but doesn't pass test

Tell us what’s happening:
When I use console.log() to check my english string before returning, it exactly matches the expected result, but fails the tests. What am I not understanding?

I’ve researched ‘strict mode’ and that doesn’t seem to be my issue.

Your code so far


function binaryAgent(stringOfBinaryCodes) {
let finalString;
decode(stringOfBinaryCodes, finalString);
return finalString;
}

function decode(stringOfBinaryCodes, finalString) {
let finalArray = [];
let asciiCode = 0;
const partialAsciiArray = [" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0,","1","2","3","4","5","6","7","8","9",":",":","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","|","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

  // change str to an array
let arrayOfBinaryCodes = stringOfBinaryCodes.split(' ');
  for(let i=0; i<arrayOfBinaryCodes.length; i++) {
    asciiCode = decodeFromBinary(arrayOfBinaryCodes[i]);
    finalArray.push(partialAsciiArray[asciiCode-32]);
  }

  finalString = finalArray.join('');
  console.log(finalString);
  return finalString;
}

function decodeFromBinary(binaryLetter) {
let binaryLetterArray = binaryLetter.split('');
let reverseBinaryLetterArray = binaryLetterArray.reverse();
return reverseBinaryLetterArray.reduce(computeAscii, 0)
}

function computeAscii(sum, currentElement, index) {
return sum + (currentElement * (2 ** index));
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Binary Agents

Link to the challenge:

I haven’t done this exercise but the first thing I notice is in the function binaryAgent you are returning an undefined variable finalString. You did not set finalString to the return value of the decode function if that’s what you meant to do.

2 Likes