Intermediate Algorithm Scripting - Binary Agents

why isn’t this working although it prints what it’s supposed to print?

function binaryAgent(str) {
  let x = str.split(/\s/);
  let y = [];
  for (let i = 0; i < str.length / 8; i++) {
    y.push(String.fromCharCode(parseInt(x[i], 2)))
  return y.join('');

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

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Binary Agents

Link to the challenge:

The problem is in your for loop.

This does not account for the spaces in the original string, so you end up with some erroneous values when you push to your y array.

console.log(y) directly before your return statement to see what I mean.

There’s a super easy fix for this though.
This line also creates an array:

let x = str.split(/\s/);

Why not use the length of this array in your for loop?


thanks I did that and it was way better

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