Bonfire 36: Bonfire: Binary Agents

TASK: Return an English translated sentence of the passed binary string.

// divide string into characters
var chars = str.split(" ");
var decimal = 0;
var msg = [];

for (var i=0; i<chars.length; i++) {
// transcode from binary to decimal
decimal = parseInt(chars[i],2);
// decode decimal and push into array
msg.push(String.fromCharCode(decimal));
decimal = 0;
}

return msg.join("");

}

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

Links:

Advertisements
Bonfire 36: Bonfire: Binary Agents

Bonfire 24: DNA Pairing

TASK: The DNA strand is missing the pairing element. Match each character with the missing element and return the results as a 2d array.

I suspect there are more elegant solutions, but this is what I came up with.


function pair(str) {

var dna = [];
var tempSubArr = [];

for (i=0; i<str.length; i++) {

if (str[i]==="G") {
tempSubArr = [str[i], "C"];
dna.push(tempSubArr);
}

if (str[i]==="C") {
tempSubArr = [str[i], "G"];
dna.push(tempSubArr);
}

if (str[i]==="A") {
tempSubArr = [str[i], "T"];
dna.push(tempSubArr);
}

if (str[i]==="T") {
tempSubArr = [str[i], "A"];
dna.push(tempSubArr);
}



}

return dna;
}

pair("GCG");

Bonfire 24: DNA Pairing

Bonfire 23: Pig Latin

TASK: Translate the provided string to pig latin.

Needed some help with the regex.


function translate(str) {

var words = str.toLowerCase().split(" ");
var pigLatin = [];

for (i=0; i<words.length; i++) {
// check first letter: is a vowel?
if ( ['a', 'e', 'i', 'o', 'u'].indexOf(words[i][0]) < 0) {

// if not, find first vowel

var firstVowel = str.search( /[aeiouAEIOU]/ );
// piggify
var pigW = words[i].substr(firstVowel, words[i].length) +       words[i].substr(0,firstVowel) + "ay";
pigLatin.push(pigW);
}

if ( ['a', 'e', 'i', 'o', 'u'].indexOf(words[i][0]) >= 0) {
var pigW = words[i] + "way";
pigLatin.push(pigW);
}
}

var pigString = pigLatin.join(" ");
return pigString;

}

translate("consonant");

Bonfire 23: Pig Latin

Bonfire 20: Diff Two Arrays

TASK: Compare two arrays and return a new array with any items not found in both of the original arrays.


function diff(arr1, arr2) {

var result = [];
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) < 0) {
result.push(arr1[i]);
}
}
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) < 0) {
result.push(arr2[i]);
}
}
return result;
}

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Bonfire 20: Diff Two Arrays

Bonfire 08: Return Largest Numbers in Arrays

TASK: Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

<pre class="wrappable">function largestOfFour(arr) {
  var maxValue = 0;
  var maxArray = [];
  for (i=0; i<arr.length; i++) {
// loop through array
    for (h=0; h<arr[i].length; h++) {
//loop through each sub-array
      if (arr[i][h]>maxValue)
//record highest value
        {maxValue = arr[i][h];}
//add value to final array
   maxArray[i] = maxValue;
  }
  }
  return maxArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Bonfire 08: Return Largest Numbers in Arrays

Bonfire 06: Find the Longest Word in a String

TASK: Return the length of the longest word in the provided sentence.


function findLongestWord(str) {
// split string into array of words and create longestWord variable;
var words = str.split(" ");
var longestWord =1;

// loop through array. if a word is longer than longestWord, replace variable value;
for (i=0; i<words.length; i++) {
if (words[i].length > longestWord) {
longestWord = words[i].length;
}
}
return longestWord;
}

findLongestWord('The quick brown fox jumped over the lazy dog');

Bonfire 06: Find the Longest Word in a String