# 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;
}
}

```

# 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.