Tag: array.slice()

Today we’re going to work on freeCodeCamp‘s Basic Algorithm Scripting challenge – ‘Slasher Flick’. This will be a pretty short post as we’re really not going to cover anything new, I just wanted to go through this algorithm to show how short and concise a solution to a problem can be.

The ‘Slasher Flick’ challenge is to ‘return the remaining elements of an array after chopping off n elements from the head’.

This is the starting point FCC gives us:

JavaScript

1

2

3

4

functionslasher(arr,howMany){

// it doesn't always pay to be first

returnarr;

}

As you can see, we need to write the body of the function ‘slasher’, which accepts an array ‘arr’ and a number ‘howMany’. slasher should then return ‘arr’ with ‘howMany’ elements removed from the head, or beginning, of the array.

If you remember from that previous post, array.slice() is a method that returns a new array (which is what we need this function to return). It takes 2 arguments. The 1st argument is the index of where the returned array will begin, which is conveniently given to us as the argument ‘howMany’. The 2nd argument for array.slice() is the index of the final element to include in the returned array, and is optional. Since we’ll be returning the entire array after cutting off ‘howMany’ elements, we’ll only use 1 argument. If you wanted to include ‘arr.length’ as the second argument to make it more clear, that would work too.

Now, given we have everything we need to return the wanted array, here’s the solution:

JavaScript

1

2

3

4

functionslasher(arr,howMany){

returnarr.slice(howMany);

}

This function now returns the given array with ‘howMany’ elements removed from the head.

Obviously this algorithm is pretty simple, it’s part of the ‘Basic Algorithm Scripting’ after all, but I wanted to cover it to show that sometimes the solution can be pretty straightforward and simple… No need to overthink it.

Today we’re going to work through another of freeCodeCamp’s Basic Algorithm Scripting challenges – ‘Title Case A Sentence’. To solve this challenge we must write a function that accepts a string, then returns that string with the first letter of each word capitalized and the remainder of each word in lowercase.

We’ll be using concepts that we’ve covered in previous posts (i.e. for loops, string methods, and arrays) and adding a couple new things like method chaining and some new string methods.

As I’ve mentioned previously, in JavaScript, the data type String has many methods associated with it. W3schools has a good intro to these methods, MDN covers the string object on a deeper level.

Quick tangent – we haven’t discussed yet that JavaScript can be tested right in your browser. If you have a question about what a snippet of code does, just plug it into the console of your browser and test it. To make sure the str.split() method that I just created is doing what I want, I plugged it into the console in Chrome and tested it.

JavaScript

1

2

3

4

varstr="I'm a little tea pot";

varwordArray=str.split(' ');

console.log(wordArray);

["I'm","a","little","tea","pot"]

As you can see, str.split(‘ ‘) is splitting the given string into an array, with each word as an individual element. Now I’m confident in continuing with the solution to the algorithm challenge…

Next, we need to iterate through the array and capitalize the first letter of each word and lowercase the rest of the word. To iterate through the array, we’ll use a for loop. To handle the upper/lowercasing of the words, we’ll need a couple new string methods.

toUpperCase() and toLowerCase() do exactly what they sound like, they return the calling string value in upper and lower cases respectively. charAt() allows you to access a specific character of a string and takes one parameter – the index of the character you want to access (indexes of strings start at 0, just like arrays).

slice() allows you to access a specific segment of a string. It takes two arguments, the indexes of where you want the segment to start and finish. The second parameter is optional, if you leave it out, the slice will start at the first index you provide and continue to the end of the string. Very important – all four methods mentioned above return the value of a string, they do not change the calling string. See code below for demonstration…

JavaScript

1

2

3

4

5

6

varstr="I'm a little tea pot";

varnewStr=str.slice(3);

console.log(str);

I'malittle tea pot//original string (str) unchanged by slice()

console.log(newStr);

alittle tea pot//slice starting at index(3) was returned to newStr

Now, as mentioned above, we can chain these methods together using dot notation, so that after one method is called, a second method is called on the returned string of the first.

We can use everything we’ve talked about to get the first character of a string and capitalize it, then get the rest of the string (starting at index 1) and lowercase it. Putting it all together looks like this:

We used the join() method, which does the exact opposite of split. join() is an array method that returns a string delimited by what you pass as an argument. In this case, we gave an empty space (” “) as the argument, so each element of the array was joined together separated by a space.

Also, you will note that we used a ‘+’ to connect 2 parts of a string together. This is an easy way in JavaScript to build a string. It can be used with both literal strings and variables as such: