Bonfire: Chunky Monkey

The Chunky Monkey Bonfire took me about 40 minutes to solve. It was a real tough one and I had to Google to find some help.  I used the .push() method and the .slice() method. Came across Wulkan’s blog in my search and his solution to the challenge really helped me find mine. Thanks Wulkan!

The Challenge:

Bonfire: Chunky Monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

For example:
chunk(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]]
chunk([0, 1, 2, 3, 4, 5], 3)
 should return [[0, 1, 2], [3, 4, 5]]


The Solution:

function chunk(arr, size) {

  var newArr = [];
  for (i = 0; i < arr.length;) {
    newArr.push(arr.slice(i, i += size));
  }
  return newArr;
 
}

chunk(["a", "b", "c", "d"], 2, "");

I was having trouble with controlling the incrementation in the ‘for loop’ so I just took out the ‘i++’ and it worked!

Advertisements

Bonfire: Truncate a string

Using .splice() to solve FreeCodeCamp‘s Bonfire: Truncate a string.

The Challenge:

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a “…” ending.

Note that the three dots at the end add to the string length.

Try it yourself before viewing my solution! If you use Google Chrome as your browser, you can right click and Inspect Element to use the console to test your code.


The Solution:

function truncate(str, num) {

  if (str.length > num) {
    return str.slice(0, (num-3)) + "...";
  } else {
    return str;
  }
}

truncate("A-tisket a-tasket A green and yellow basket", 11, "");
truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);

Bonfire: Repeat a String Repeat a String

Three in a row! Are these challenges easier than the first Bonfires or is all this training really starting to improve my programming brain? (I hope it’s the latter!)

This is my solution to FreeCodeCamp‘s Bonfire: Repeat a string repeat a string. This one was much easier to complete as you only need to check if the num argument is less than zero.

The Challenge:

Repeat a given string (first argument) n times (second argument). Return an empty string if n is a negative number.

The Solution:

function repeat(str, num) {

  if (num < 0) {
    return "";
  } else {
    return str.repeat(num);
  }
}

repeat("*", 3, "");
repeat("abc", 3, "");

Bonfire: Confirm the Ending

On a roll! Just did two FreeCodeCamp Bonfire Algorithms back-to-back. This one took me about 20 minutes. Use .substr() method.

The Challenge:

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

The Solution:

function end(str, target) {
  var lastLetter = str.substr(-(target.length), target.length);
  if (target === lastLetter) {
    return true;
  } else {
    return false;
  }
}
end("He has to give me a new name", "name")
end("Bastian", "n", "");

Use console.log(lastLetter); after defining the variable lastLetter to see what is printing. It really helps to see what is going wrong with your code.

Bonfire: Return Largest Numbers in Arrays

Took me about an hour, but I finally passed the Bonfire: Return Largest Numbers in Arrays from FreeCodeCamp’s Algorithm section. Whoohoo!

The Challenge:

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

The Solution:

function largestOfFour(arr) {

  var newArray = [];
  var x = 0;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (x < arr[i][j]) {
        x = arr[i][j];       
      }
    }
   newArray.push(x);
    x = 0;
  }
  return newArray;
}


largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

There is a much easier way, by using the math.max.apply method, but I didn’t choose to use it.