These are chat archives for camperbot/bonfire-binary-agents

25th
Oct 2015
Ihab Baghdadi
@ijb87
Oct 25 2015 11:52
bf confirm the ending
CamperBot
@camperbot
Oct 25 2015 11:52

:fire:Bonfire: Confirm the Ending :link:

function end(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  return str;
}

end('Bastian', 'n');

Check if a string (first argument) ends with the given target string (second argument).

more info:  bf details | bf links | hint

Ihab Baghdadi
@ijb87
Oct 25 2015 11:52
hint
CamperBot
@camperbot
Oct 25 2015 11:52

hint [1/7]

## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ihab Baghdadi
@ijb87
Oct 25 2015 11:52
hint
CamperBot
@camperbot
Oct 25 2015 11:52

hint [2/7]

Explanation:

This problem is very straight forward, you will get string that will represent a sentence in binary code, and you need to translate that into words. There is not direct way to do this so you will have to translate twice.

You should first convert from binary to decimal and from decimal before translating those values into characters

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ihab Baghdadi
@ijb87
Oct 25 2015 11:52
hint
CamperBot
@camperbot
Oct 25 2015 11:52

hint [3/7]

Hint: 2

Things are easier when focusing on smaller parts, divide the input to focus on one letter at the time.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ihab Baghdadi
@ijb87
Oct 25 2015 11:53
hint
CamperBot
@camperbot
Oct 25 2015 11:53

hint [4/7]

Hint: 3

Make sure that each time you transcode a character from binary to decimal, that you reset whatever variable you used to keep track of the ones. Also do not forget to turn everything back into one string.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Hamid Edouiki
@hamidos
Oct 25 2015 22:53
hint
CamperBot
@camperbot
Oct 25 2015 22:53

hint [5/7]

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Hamid Edouiki
@hamidos
Oct 25 2015 22:54
hint
CamperBot
@camperbot
Oct 25 2015 22:54

hint [6/7]

Code Solution:

function binaryAgent(str) {
  biString = str.split(' ');
  uniString = [];

/*using the radix (or base) parameter in parseInt, we can convert the binary 
  number to a decimal number while simultaneously converting to a char*/

   for(i=0;i < biString.length;i++){
   uniString.push(String.fromCharCode(parseInt(biString[i], 2))); 
  }
//we then simply join the string
  return uniString.join('');
}

Code Explanation:

  • Separate the string into an array of strings separated by whitespace.
  • Create some variables that will be needed along the way, the names are self explanatory for the most part.
  • Iterate through each binary string in the new array.
  • Convert to decimal by using parseInt(binary, 2) (with the second parameter we tell in which base our numbers currently are)
  • At the end, we return out converted message.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Hamid Edouiki
@hamidos
Oct 25 2015 22:55
hint
CamperBot
@camperbot
Oct 25 2015 22:55

hint [7/7]

Alternative Solution:

function binaryAgent(str) {
  // Separate the binary code by space.
  str = str.split(' ');
  var power;
  var decValue = 0;
  var sentence = '';

  // Check each binary number from the array.
  for (var s = 0; s < str.length; s++) {
    // Check each bit from binary number
    for (var t = 0; t < str[s].length; t++) {
      // This only takes into consideration the active ones.
      if (str[s][t] == 1) {
        // This is quivalent to 2 ** position
        power = Math.pow(2, +str[s].length - t - 1);
        decValue += power;

        // Record the decimal value by adding the number to the previous one.
      }
    }

    // After the binary number is converted to decimal, convert it to string and store
    sentence += (String.fromCharCode(decValue));

    // Reset decimal value for next binary number.
    decValue = 0;
  }

  return sentence;
}

Explanation for the second solution

  • For each of these binary strings, check for the ones and ignore the zeroes.
  • For those that are one or active then convert them to decimal, this takes into account the position and the right power it needs to be raised to.
  • Store the power into the power variable by adding it to any previous ones on the variable decValue. This variable will add and add the powers of the active ones until the end of the loop and then return the decimal number.
  • Convert the final decimal outside of the inner loop and then convert it to ASCII and saving it to sentence along with any other text string already converted and stored.
  • Reset the variable decValue to avoid getting wrong decimals before continuing to the outer loop.

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @JamesKee

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)

type hint for next hint :pencil: [Contribute at the FCC Wiki]