Binary agents, looks solved but won't pass?

Hi all, it looks like the correct results are returned both on my laptop and in the FCC console. I tried both document.write and console.log.
So I’m unsure how to fix it as I don’t see what the problem is?
Hope someone can help? You’ve all been very helpful so far :slight_smile:

Your code so far


function binaryAgent(str) {
 if(str.length >8){
		//more than one character.
		let strArr=[];
		let nexti=0;
		let lastindex=0;
     //length of number of characters.
	    for(;nexti<str.length;){
			nexti = lastindex+7;
			let tempStr=[];
     //each character's bits is pushed into array
			for(let j=lastindex;j<=nexti;++j){
			    tempStr.push(str[j]);
			}
     //save location from where to continue
			lastindex= nexti;
			//skip past space.
			lastindex+=2;
			let shortStr= tempStr.join("");
			let decimal = parseInt(shortStr, 2);
		    let asciiStr = String.fromCharCode(decimal);
			strArr.push(asciiStr);
			
		}
   //convert array back to string type
		str = strArr.join("");
		return str;
	} else {
		//just one character.
	    let decimal = parseInt(str, 2);
		str = String.fromCharCode(decimal);
		return str;
	}
 
 
 return str;
}

console.log(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 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Binary Agents

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents

You have an extra character at the end

binaryAgent(
    '01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111'
  ).length
// 22

"Aren't bonfires fun!?".length
// 21

Sorry, I’m confused, I don’t see the extra character? in the binaryAgent(“01000001 …”) ; line ? I count 21 character bytes ?

You don’t see it because it isn’t a visible character.

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

retString[retString.length - 1].charCodeAt()
// 0

String.fromCharCode(0)
// " "

Edit: Just to be clear. The problem is not with the binary data. It is with your code.

Thanks :slight_smile: I can see now the loop doesn’t exit when it should, easy enough to fix.

I wrote the code like that

Binary Agents

function binaryAgent(str) {
var newArr = str
.split(’ ‘)
.map(bin => String.fromCharCode(parseInt(bin, 2)))
.join(’’);

return newArr;

}

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

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Also, this is a nine month old post you’re commenting on, I’m not sure how helpful the answer is likely to be to the OP.