Binary Agents Help (Solved)

Binary Agents Help (Solved)
0

#1

So i’m trying to convert the Binary Agents Binary to text challenge, here is my code:

Repl --> https://repl.it/@John_Nicole/Converting-binary

function binaryAgent(str) {
    str = str.split(" ")
    // splits so i can get each bit of binary
    var values = [1, 2, 4, 8, 16, 32, 64];
    // values for binary (powers of 2 as you go up)
    var letterBinary = 0;
    // this is where i will total the value of the binary bit
    var sentence = [];
    var check = []
    for (var i = 0; i < str.length; i++) {
        str[i] = str[i].split("").reverse()
        // go through, reverse and grab each bianry bit
        for (var j = 0; j < str[i].length; j++) {
            // checking each number. I start at 1 since the first digit is ignored
            if (Number(str[i][j]) === 1) {
                // if the current number is a 1, we will find its value in values
                letterBinary += values[Number(str[i][j])];
                // adding that value to the sum
            } 
        }
        check.push(letterBinary)
        sentence.push(String.fromCharCode(letterBinary));
        // turning that number to hopefully a letter
        letterBinary = 0;
        // reseting the value of the binary each bit of binary
    }
    console.log(check)
    return sentence.join('');
    // return finallized sentence
}

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

My code loops through each section of binary and check if its a 1 or a 0. If its a one it adds its corresponding value in the values array. You can go through more in depth if you read the comments.


My thing is, why am i getting:

'\u0002\b\u0006\n\u0006\b\u0002\u0006\n\n\b\u0006\b\u0006\b\u0002\b\b\n\u0002\n'

As my output? I thought that ‘String.fromCharCode’ returned a actual letter or number.


#2

String.fromCharCode expects an ASCII code and will return the applicable character. You need to put a console.log statement to check your letterBinary values. They are numbers but ASCII code values for the letters A-Z are 65-90. You code is producing letterBinary values like 2, 6, 8, and 10. Those strange string characters are what gets returned from those 2, 6, 8, and 10 codes.


#3

? I fixed my code too, it was starting from the 1st character instead of 0nd. I put in a check array to see all my letterBinary's. Plus letterBinary is a number in my code, not a letter.

I think there’s a problem with my code. 00111111 which is turned into 11111100, outputs 12. That seems low. I think it should return 63 because 1+2+4+8+16+32, which is a question mark in Aren't bonfires fun!?.

I use letterBinary += values[Number(str[i][j])]; to access the array of values. I did

the current Binary clip (i) --> How far i was into the binary clip (j).

Why am i getting 12 instead of 63? Why aren’t i converting correctly? (still confused on your explanation)


#4

I would definitely consider renaming letterBinary to asciiCode, because that is ultimately what it will be when you get your logic correct.

The problem with your logic is:

if (Number(str[i][j]) === 1) {
  // if the current number is a 1, we will find its value in values
  letterBinary += values[Number(str[i][j])];
  // adding that value to the sum
} 

You only are adding the 2nd element of values to letterBinary during each iteration if Number(str[i][j]) equals 1. You already know Number(str[i][j]) is equal to 1, which is why it is executing the code within your if true block, so why are you using Number(str[i][j]) as your values array reference?

00111111 outputs 12, because when your reverse it, it becomes 111111100. Each time you have a one showing, you are add the number 2 to binaryLetter and there are 6 1s in it so 2x6=12.


#5

Change

letterBinary += values[Number(str)[i][j])];

to

letterBinary += values[j]

You want the position where you find a “2”. Right now you’re just adding the value “2” (after converting it to a number) to your letterBinary value instead of the values from the values array.


#6

You can use parseInt to get the values of the binary, like this

parseInt("00111111", 2)


#7

@iamknox Thanks, i don’t know how i missed that! :smiley:

@RandellDawson - I knew it was one, instead of finding its relative position in the values, i was going to the 2nd item. I was just saying values[1] --> (2) instead of j which was how far i was into my binary clip. That is why 6 was 12. Thanks any ways for your help, you did catch a error in my code in your first reply :smiley:


#8

Sometimes it just takes another pair of eyes.


#9

Yeah, i watched a tutorial on how to convert Binary to English where they did exactly this but by hand and looked the number on the ASC|| chart/ key. New code btw:

function binaryAgent(str) {
    str = str.split(" ")
    var values = [1, 2, 4, 8, 16, 32, 64];
    var letterBinary = 0;
    var sentence = [];
    var check = []
    for (var i = 0; i < str.length; i++) {
        str[i] = str[i].split("").reverse()
        for (var j = 0; j < str[i].length; j++) {
            if (Number(str[i][j]) === 1) {
                letterBinary += values[j];
            } 
        }
        check.push(letterBinary)
        sentence.push(String.fromCharCode(letterBinary));
        // turning that number to hopefully a letter
        letterBinary = 0;
    }
    console.log(check)
    return sentence.join('');
}

binaryAgent("00111111");

#10

@r1chard5mith , what does the 2 mean?


#11

It means Base 2 (binary). Base 10 is decimal.


#12

function binaryAgent(str) {
return str.split(" ").map(x=>String.fromCharCode(parseInt(x,2))).join(’’);
}