JavaScript the Basics for Beginners - Section 7: Algorithms | Rob Merrill | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

JavaScript the Basics for Beginners - Section 7: Algorithms

teacher avatar Rob Merrill, JavaScript Instructor

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

7 Lessons (53m)
    • 1. JavaScript the Basics - Functional Programming: Promo Video

    • 2. JavaScript the Basics - Functional Programming: Identify Unique String

    • 3. JavaScript the Basics - Functional Programming: Longest Word in a String

    • 4. JavaScript the Basics - Functional Programming: Permutation of Two Strings

    • 5. JavaScript the Basics - Functional Programming: Sum of Range

    • 6. JavaScript the Basics - Functional Programming: Remove Elements from Head

    • 7. JavaScript the Basics - Functional Programming: Palindrome

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Course 7 - JavaScript Algorithms

In this course you will cover: 

  • Indentify Unique Strings
  • Identify Longest Word in a String
  • Permutation of Two Strings
  • Sum of Range
  • Remove Elements From Head
  • Palindrome

Through our whole JavaScript the Basics for Beginners course series you will find this is your one stop necessary to enter into the language of JavaScript. We are going to start from scratch and build up our understanding of the world's most popular programming language together. 

View JavaScript in action with whiteboard videos optimized for viewing on your smartphone. This is truly a course that you can engage with no matter where you are. Take your JavaScript eduction on the go! With whiteboard videos you get to visualize what JavaScript does under the hood. 

You can practice JavaScript in your web browser! Take advantage of Google Chrome developer tools to put the JavaScript you have learned during our whiteboard lesson into practice with further examples and challenges. This is our JavaScript playground.

Learning JavaScript is like learning a language. This course won't make a false promise of saying you will master JavaScript overnight. However, this course will immerse you into the world's most popular programming language. 

Each course will introduce you to something new in JavaScript :

  • JavaScript Syntax
  • JavaScript Building Blocks
  • JavaScript Best Practices
  • JavaScript Objects
  • JavaScript Arrays
  • JavaScript Regular Expressions
  • JavaScript Functional Programming
  • JavaScript Algorithms
  • JavaScript Data Structures
  • JavaScript ES6 Syntax

You'll finish this course with an understanding of JavaScript that will let you take a deeper dive into the language and possibly launch you into a new career as a JavaScript web developer.

Learning a new language can be difficult and even feel intimidating. JavaScript the Basics makes the the learning process feel accessible and welcoming!

Meet Your Teacher

Teacher Profile Image

Rob Merrill

JavaScript Instructor


Rob Merrill is a Full Stack JavaScript Developer. His specialty is in the MEAN stack focusing on AngularJS, NodeJS, ExpressJS and MongoDB.

He is a graduate of Code Fellows Advanced Full Stack JavaScript Development course and has been developing with JavaScript for three years.

In his spare time he travels around Seattle's finest craft breweries with his wife and their dog.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. JavaScript the Basics - Functional Programming: Promo Video : Welcome to Java script. The Basics. JavaScript is the most popular programming language in the world has been called the language of the Web Stack Overflow listed as the most popular deaf technology and get have numbers it as the top ranks language learning JavaScript will allow you to be a full stack developer or fund a specialty on the front end or the back end. With JavaScript, you'll be able to cope with front and frameworks like react and angular on the client side . And you can also cut on the back end server side with no Js and create databases with Mongo . DB JavaScript is a language, so it's going to take some time to learn before you can write poetry or a novel in any language, you have to first learn the languages alphabet, how to make words and see how others use it. To learn JavaScript. We're gonna look under the hood. This isn't just going to be an exercise in copying someone else's code to wind up with an application you don't understand. Everything we do will be visual in the form of a whiteboard lesson where you can really see how everything works and connects. If you are a visual learner, this is the course for you. This isn't about going 0 to 60 or zero to hero, because if you really are starting at zero, those courses leave you behind quickly. It's about learning. Understanding and applying is that you could build and create with JAVASCRIPT. Way will then follow our white board. Lessons into the lab way will enter into the Google Chrome software developer. Consul thistles are Java script playground. There is nothing you will need to download for this course. No special tools, no extra steps. Just Google, chrome, everything. You learnt the lesson we will then recreate in the lab together. I'll then give you a code challenge to recreate what we learned together and then we'll look together at a possible solution. Welcome to Java Script. The basics. Let's start speaking job script 2. JavaScript the Basics - Functional Programming: Identify Unique String: All right, welcome back to Java. Strip the basics in this section, we're going to be looking at algorithms. What is an algorithm in mathematics and computer science and algorithm is a self contained , step by step set of operations to be performed algorithms perform calculation, data processing end or automated reasoning tests in each of our lessons will begin with raising the question that we're trying to solve. Well, then share with you list of methods and properties that you could use in funding. A solution in our whiteboard session will demonstrate these tools in action. Then we'll head straight to the lab. From there, you will have the opportunity to pause the video and give yourself the chance to solve it yourself during the lab. All right, out some pseudo code to assist you if you need a framework to get started, kind of like bumpers and bumper bowling. Before we look at the solution, I'll ask you to set the timer for 15 minutes. Then I'll walk you through a solution or two. Let's start with the first question. I want you to implement an algorithm that will determine whether a string has all unique characters in other words, it has no repeat characters. Three ingredients you may find useful in solving this question are a for loop, a conditional statement, the string method index of and the string of method last index off. Now we already know how a four loop works way have the key word for followed by parentheses , with three statements, the initialization of the counter, then the test condition. This will then lead us into the code block as long as the second statement. The test condition is true way, then running code block. And after that we update our counter, which is statement three in our for Lou. That leads us back to the second statement into the test condition to ask if it is still true. We keep entering the code block until the test condition is false. We've also looked at conditional statements in our first loop lesson are conditional Statements will have the basic syntax of the if keyword, followed by a set of parentheses with an expression within them. If that expression evaluates to true, we will step into the code block, which is surrounded by a pair of curly braces. If the expression evaluates to false then you will step over the code block. Now let's look at the string method index off. We have seen string methods before. For instance, string dot to upper case turns all the characters in our string into an upper case in this example are string of car that is, an all lower case is turned into all uppercase with the to upper case method. The string method index off will return the first index in which we find a match for the string that were searching for Let's see it in practice, I will declare a variable called Khar. We'll start at the value of the string Honda Civic. Now suppose we want to find the placement of the word civic in our string. We would write car are previously declared variable than the method of index of then the method of index off and then we'll pass to it. The argument Civic. We get a return value of six. This is the first index in which our match begins with the letter C. If we were to try the same method with the string civil, which is almost a match matching C I V I, we get a result of negative one, meaning that there was no match for our string that we were searching for. Let's do one more check. Suppose we were looking at the index of the string character I. Even though there are two eyes, we will only return the first I at index seven because this method returns the first value . That is a match. Now let's look at another string method you may choose to take advantage of. This is last index off. You could probably safely guess that last index of is going to give you the index of the last place that you find a match. Let's look at our earlier example index of car. I returned the first I at index seven last index of car. I will return the last I at index nine. All right, time to go to the lab. Now, if you want, you can pause this video right now and try to work out a solution. If you hold on a minute more, I'm going to walk you through pseudo code. This is me writing out in English approach that we can take to solve with coat. I would suggest that if you have no idea where to start or have struggled already for a few minutes to take a look at the pseudo code. Then give it a shot again. First, we're going to create a function. You can name it. Whatever you want. You are going to give it a parameter of whatever argument or string you're going to check. This will then lead us into a code block in which we're going to examine our string to see if all the characters are unique. We will use a for loop to iterated through the string. You'll need to fill the four loop with your counter initialization, your test condition and your counter update. Assuming your test condition is true, you will jump into the next code block. This will have a conditional if statement we're going to look and see if the first index in the last index match. If they do, we can iterated through the next letter in our array. If the condition evaluates to false, this means that the letter must appear twice if there is a different first appearance and last appearance. If that is the case, who want to return false and we will receive a false statement. If we make it through a whole string with no false statements, then we'll jump out of the co block and return. True to test our string will name our function we have previously defined and fill it with an argument that we want to test. All right, go ahead and set a timer for 10 or 15 minutes and pause the video and give it a shot. Then we'll come back together in a few moments and come up with the solution. All right, How to go. Let's jump into the Google Chrome Consul and come up with a possible solution. If you didn't come up with a solution, that's OK. Remember, the learning is often in the struggle. Through trial and error. You're learning what works and doesn't work. We'll go ahead and declare a function called unique. We'll give it a parameter of str for string will then make a code block that will enter into when we call or invoke our function And this code block, who will create a four loop that will iterated through the string testing. If each character is unique, we will ask if the character we're looking at in the current index. Whether the first instance of that character is not equal to the last instance of a character that would mean a repeat. So this conditional statement is saying, If it is not true that the first instance is equal to the last instance, then this is a true conditional statement, and we will return false. However, if the first, unless instance, are the same than it is untrue that they are not equal. So we don't return the false statement and go back into our four loop and look at the next string character. The logic might be a little tricky if you have to pause the video and diagram that a bit for to sink in. That's OK, Let's look at the argument car. If we put car in as our argument, we see that C is equal to the first instance and the last instance. So it is not true that they are not equal, so this conditional statement is false, so we don't return false. We go back into our four loop. We look at the letter a and see the same result as when we looked at sea in the zero index so that leads us to our four loop again with index to and the letter R with the same result , we update our counter by one and now our statement to the test condition. We have the counter three, which is not less than the length of our string, which is three. So we jump out of the code block, never hitting the false return value at this point. As we have stepped over the code block, we return True. Now, if we look at another example of the string ad, we see that when we have reached our index of one the second string character of D, that it is true that the first index of D is not equal to less index of D, which is that index too. So this will cause our conditional statement to evaluate to True. Now we move on to our code that returns the value of false. There you go. If you have a few moments, go ahead and try out the following arguments. Java script, smartphone, beer chair or whatever else you want. Thanks for taking this first look at javascript algorithms will see you in our next lesson with our next question 3. JavaScript the Basics - Functional Programming: Longest Word in a String: All right, welcome back to Java. Script The basics. In our last lesson, we begin to look at algorithms and JavaScript. An algorithm and a computer is a lot like a recipe and tells your computer precisely what steps to take to solve a problem or reach a goal. The ingredients are called inputs, while the results are called the outputs. In our previous lesson, we looked at an algorithm to determine if all the characters in the string are unique with no repeats. Our question for this lesson is this. Given the string, find the length of the longest word, all working through two solutions. Both of them will involve using a for loop. A conditional statement, as we saw in our previous question, however, will also use the string method split. We'll take some time to look the string split method. Last lesson. We looked at the String index of and last index of method. This method on the string will split a string into an array of substrates. You will be able to pass arguments into the parentheses to determine how many sub string elements in the array it will turn your string into. So let's take our car from our last lesson are Honda Civic. When we attach are split method without an argument, we get an array with an element at Index zero, The string Honda Civic. When we attach are split method with an argument of an empty string, we get an array that gives every character in the string a sub string in the array, including the white space. If we touch our split method with an argument of a string with the space, we get an array with two elements. Honda and Civic. All right now we have all the tools we will need to head into the lab to find a possible solution. Again, the question is, create an algorithm that funds the longest word in a string. Go ahead and pause the video and give it a shot. Or, if you need a little more guidance, hang on for a moment and we'll walk through some pseudo code. We're going to declare a function. This is what will pass our string of words into as an argument. This will lead us into the code. Block here will use the string method. We just learned to split our array into an array of words. Think through what the argument you will need to pass into the stream method that we just learned will also want to declare a variable that will store the value. The longest string. Just a hint. You'll probably want to start at zero. Well, then create a four loop that will loop through this array that we have turned our string into, As always, that will have three statements. Will initialize that counter, establish our test condition and update our value. As long as we passed, our test condition will go into our code block, which will have a conditional if statement. This will take some brainpower, but you're going to be comparing your current index word length to the variable you declared earlier and the value assigned to it. If it is true when you have finished iterating through your array, you will return the value of the variable handling the greatest length. You'll then want to invoke the function with your string argument. Set your timer for about 10 to 15 minutes. If you solve it fast. Great. If you can't get through it, that's fine. To just make a much progress is possible. It will help the solution connect when we finally get to it. All right, How to go? Let's take a look at a possible solution. We'll start by declaring a function called Max. Word will give it a parameter of string. Str this will later be filled with the string argument. Next, we'll go ahead and declare a variable for array. We will call it words. Well, son, at the value of the str with the split method in the parentheses, we will put quotes with a space. If we didn't put a space, we would have an array filled with sub strings of characters instead of an array of words. This is going to be there, Ray, that we will iterated through with our four loop in a moment. Next with the Clara variable called biggest word will store the value of the largest word here We will initialize the value of this variable with zero. Now we will create our four loop that will iterated through our array that we created called words. We will initialize our value of I 20 then set our test condition to be that eyes less than the length of our words array with our property length. This sends us into the code block. When we come out of the Kobach will update the value by one inside of our code block. We have an if statement within our parentheses will put an expression that if it evaluates to true, we will move into another code block. So if our word at our current index is greater than the biggest word initially set to zero that in the code block where to assign that value to the biggest word. So the next time we come up on this expression will compare our current index with our new biggest word. When we have finished going through a loop, we will exit out, and our next section of code will return the number value of the biggest word. So now let's go ahead and try it out. We will invoke CarMax word function and give it the argument of the string. My favorite day is Friday. Are string split method will turn this into an array of five words. We'll loop through this array. Our biggest word is initialized to zero R four loop is going to go through this loop are conditional statement is going to ask if our current index at this 0.0, which is the string my, whose property length is equal to 22 is greater than zero. So our biggest word is now set to to I would jump back into our co block, looking at our conditional statement. Our current indexes one the string favorite, the property length of favorite is eight. A is bigger than the current value for biggest word, which is to So we move into the code block, which is a signing biggest word, the number value of the length of the current index. Eight. We do this for the rest of the loop. Another day of three, or is of two or Friday of six will be larger. When we invoke our function, we will get back the number value of A as our biggest number. All right, hope that solution was a help to you. Let's go ahead and move through. Another solution this time will move a little faster. First will declare a function called Max Word and give it a parameter of str, which we will pass our argument well. Then declare a variable code words and store in it an array with string split method on whatever argument ISS. What we'll do different this time is instead of declaring a variable called biggest word and assigning that the value of zero will assign it the value of an empty string, well, then create our four loop, just like in the previous solution. When we enter into that code block, our if statement will have an expression that will be different than our previous solution as well. Will ask if the property length of our current string in the index is larger than the property length assigned to our biggest word, variable, which at this point an empty string would evaluate 20 So we go back into our code block as before, and the biggest word is now equal to this new string. At the current index. We repeat this process through a four loop and have a final string assigned to our biggest word way. Then return the number value of biggest word with the length property. Let's go ahead and put in that same string as an argument. There you go. Thanks for joining me in looking at creating an algorithm to determine the longest length of a word in a string, we'll see you in our next lesson for next question 4. JavaScript the Basics - Functional Programming: Permutation of Two Strings: All right, Welcome back to Java. Script the basics. We're continuing our lessons. Looking at JavaScript algorithms, we've already covered creating an algorithm to determine if a string has all unique characters and another algorithm determining the length of the longest word and a strain. We talked about how algorithms are like recipes. Inputs are unlike ingredients and they give us an output. This lessons question is to create an algorithm to determine if two strings are a permutation of each other. How do we know if a string is a permutation of another stream? We can do that by checking that a string contains the same characters and the same amount of those characters, no matter what the order. So baseball and ball base and A s a l l b b e r o permutations of each other. They can all be used to spell different things, but they contain the same characters they'll have to B's to A's two l's and one e. Some tools at your disposal to create this algorithm are string split method, a raise sort method, an array join method in our previous less, and we looked the string split method. You can go back and look at our algorithm on determining the longest word in a string if you need a refresher. So let's go ahead and move forward and look at the sort method on an array. The sort method sorts the elements of an array in place and returns theory. A. It doesn't just return an array value. It mutates. What the array waas. The array is sorted according to each characters Unicode code point value according to the string conversion of each element. We won't go into Unicode in detail right now, and it can be a bit tricky when it comes to sorting an array of numbers. Let's look at an example of the sort method on strings in an array and on numbers, and nunnery will declare a variable called ST and the Sun that the elements of California, Washington and Arizona. The result is that we return an array with Arizona, First, California Second and Washington third. Hopefully, that is pretty intuitive. In alphabetical order, we look at the first letter to determine order. We know this from how roll call is often taken in classes. We don't pay attention to the following letters or length of a word unless the first letter is a match and we need more precision when we look at numbers will take a little bit of a different approach. It isn't as intuitive but should sink in quickly. When we think of lining up numbers in order in real life, we take into account all of the values and all of the digits in a building. The first floor comes before the fifth floor, and the fifth floor comes before the 10th floor. However, because we're working with Unicode Point value according to string conversion, the string of 11 actually comes before the string of three in Unicode Point Order. Likewise, the string of 111 will come before the string of three justice. The string of 333 will come before the string of five and Unicode Point order again. Don't worry too much about sorting numbers. According to Unicode value. We won't need it for this algorithm. I just wanted to introduce you to that as we talk about the erase or method now back to solving our algorithm, Why are we turning our string into an array? Why not just sort a string. We'll need this array sort method for a question, because we don't have a sort method for a string in Java script. So we're going to have to turn our string into an array of sub strings, which you learned before with the string split method. And our array object has a method that can then sort these sub strings. Okay, now we have a handle on the sort method on an array. Let's go ahead and look at the array method Join. This is going to be similar to the string method split as the string methods split turns a string into a sub string of elements and an array, the array method joined turns those sub string elements in an array back into a string. Let's look in an example. Let's suppose we have a car variable will sign it. The value of the string. Honda Civic will use the split method with an argument of an empty string. This will turn our car into an array of sub strings for every character we can then just as easily put this array of sub strings back into a string with the joint method on the array , we just insert an empty string as an argument to come up with same original value. Let's look at one more example with our car. If we do the split method on our car with an argument of string space, we will return a value of an array with two sub string elements the string Honda at the element of Index zero and the String Civic as a element at index one. To put that string back together, we used the array method join and passed to it a string with a space in it. In this question, you want to focus on what you put within those parentheses to get our desired results. The other key to this algorithm is going to be What are you going to put between the split method and the joint method? All right, you have all the tools to figure out whether two strings are permutations of each other. Let's go to the lab. You have everything you need to know to solve this question. Go ahead and give it a shot if you're ready. Even if you're not ready, pause the video, wrestle with the question for a few minutes. Try to wrap your mind around some of the possible solutions, then come back and I will walk you through some pseudo code to give you a little more assistance if you need it. All right, here's some pseudo code to get you started. We're going to want to declare a function and give it a name that we can call later. We will then give it to parameters. We will feed the two parameter strings that we want to compare to see if they're permutations of each other. Well, then enter into our code block. At that point, we will compare the two arguments that we pass to the parameters. We'll use the split sort and join methods we learned earlier and will return the value of that comparison. And that's it. I will just invoked the function with their two arguments that we want to evaluate whether they're permutations of each other or not. Now that you've had a little assistance, go ahead and pause the video and wrestle with implementing the real code in place of the pseudo code. As always, set a timer. Don't settle for watching the solution alone. Go ahead, pause the video and we'll reconvene in a few moments. All right, how to go? Let's go ahead and come up with a possible solution. We'll start with declaring our function. Naming it is permutation and give it to parameters of Str one and str too that will send us into the code book. We're going to return the value of the comparison between those two strings using the strict inequality operator on each argument, we will split it into an array of sub strings for each character in our string argument. We'll use the sort method on that array so that all the character elements are in the same order in the array. Then we'll use the joint method to bring all those array elements together into a strength . Now, it's important that for our split method, we use an empty string to create our new sub string of in a race. And when we use our join method on the array that we use the argument of an empty string to bring those sub strings back into one string. Now quality operator can let us know if those two are identical. I will go ahead and color function with any two arguments that we might choose to give it. We'll go ahead and give it to arguments. The string voluble and then another string with the characters. L l B Y a O L L VE. And the result is true. Both have one. A one B four Els won ho one V and one y All right, That's all we have for now. Thanks for taking a look with me at creating an algorithm to determine of two strings or permutations of each other. We'll see you in the next lesson with our next question. 5. JavaScript the Basics - Functional Programming: Sum of Range: Welcome back to Java script. The basics and this lesson we're going to continue our section on creating algorithms are current challenge is to create an algorithm that will find the sum of all numbers within a range. We will suppose we were given an input of two numbers in any order. For example, let's say a one and a four. This would be a range of 1 to 4. It would be like one plus two plus three plus four Equalling 10. Let's suppose that the largest number is given first, let's say five and a two. This would be a range of two through five that would be two plus three plus four plus five for a total of 14. I'm going to go ahead and provide you with the possible solution. In a few minutes, we will need to know how to use the math thought Max Method and the math dot men method and a four loop thrown in there as well. First, we'll look at the math dot max method. The Math Max method will look at all the elements you haven't your array and return the value of the largest number. The math thought Max Method returns the largest of zero or more numbers. Let's go ahead and look at an example, and our first case, 20 is clearly greater than five, followed by a negative eight. Being larger than negative 20 and 25 as greater than negative 10 should be a pretty easy concept so far, and our next function math that men will be similar in nature. Let's quickly look at math dot men. The math dot men function returns the smallest of zero or Mara numbers. Let's look at some code. We have variables X and why with values of 10 and negative 20. When we feed those values to our math dot men method, we return a value of negative 20. All right, you have all the tools that you'll need to solve the problem. Let's head to the lab. You can go ahead and pause the video now and wrestle with this problem. If you want to wait to see the pseudo code, I'm going to provide, maybe pause the video first and try to write up your own pseudo code in your own words. What do you think might be a possible pathway toe work towards a solution. All right, let's go ahead and get started with writing. Some pseudo code will begin by declaring our function. That will take in an array of two elements that we will want to discover the some of those values and all the numbers in between their range. We're then going to set three variables Toe one. We will sign the max value of our two array elements. You're going to have to make sure to target the index of each element. We will have another variable to set the men value of our two array elements and 1/3 element sitting a temporary value for a starting point that we will then return when we have reached our total value, will then create a four loop that will start at the men value and iterating up Teoh and include the max value. This is a little different than anything we've done before, but wrestle with it and you should be able to make it work are four loop will update our temporary value. That will be our final result that we will return after we have gone through our loop and stepped over the code block. Then we will call our function with our array of any two elements to find the some of them and all values in between. So go ahead and pause the video, give it a shot, take 10 to 15 minutes and try to work through the code yourself. All right, how to go. Let's go ahead and look at a possible solution. We'll go ahead and declare function and call it at all. I will give it a parameter of array. Well, then enter into our code block. Let's create a variable that will give us our array element with the max Value. Will do that by sending Max the value of math. Thought Max and passed to it the argument of our index. Zero of our array. An Index, one of our array, which are our first and second elements. This will assign Tore Variable Max the largest number of our array. Then we will repeat the process on Lee with the math dot men method to give us the element with the smallest value finally will create a variable that will hold the total of this, some of our values and we'll started at zero. Next, we'll create our four loop with three statements. The first is the initialization of our counter, which will be the array element with the smallest value. Then we'll set our test condition Well, iterated by our update value of one, as long as our counter is less than or equal to or largest number and the array, as long as this test condition is true, we will enter into the code block. Now, our total value will add the value of the current number were looping through. This will lead us back to the four loop and repeat the process until our counter is larger than the value of the variable. Max will then step over the code and return the total value. So now if we call our function at all and give it the array, All right. If you have some time, put in some other arguments into your function and see what you get. Put the largest number first or experiment with negative numbers as well. Thanks for joining me. In this challenge of creating an algorithm, they confined the some of the range of two numbers. We'll see you in our next lesson with our next question. 6. JavaScript the Basics - Functional Programming: Remove Elements from Head: All right, Welcome back to Java. Script The basics. We're continuing our deep dive into algorithms as we're going through. These algorithms were hopefully reinforcing syntax, methods and logic. We've already learned in the course we're also trying to add in content with each lesson that may be new to you and giving you a brief teaching on the subject than practicing it and implementing it in creating an algorithm. Given an array of elements, let's go ahead and look into this lessons algorithm challenge. Given an array of elements, we want to create an algorithm to return their value. After cutting off X amount of elements from the head, for example, suppose were given an array with a car, a boat and a bus. We will be given a number, for example, one that we will want to chop off that number of elements from the front of our array. So our function would return an array with two elements boat and bus. Our main tool to solve this algorithm is the spice method in an array. One challenge in using the spice method on an array is to be able to differentiate between the spice method and the slice method. Their names were so similar that it is easy to mistake the two. The sliced method returns a new array it does not alter the array that the method is applied to. It would be like taking a slice from an orange, you know, have the slice as something new, and your original orange is still fully intact. If we have an array called Vehicle with the Elements Car, Boat and Bus and we do the slice method removing one element from the head, we would return new array with that element. But the vehicle array has been left untouched. The spice method actually alters or mutates theory by removing existing elements and or adding new elements. Let's look at some syntax. First, we have the starting point. This is the index at which to start changing the array with an origin of zero. If negative and remember from the end, we don't start counting at zero, we start counting at one. Next we have the delete count. This will be an integer indicating the number of old array elements to be removed. Lastly, we have the additional elements. The's air, the elements toe add to the array beginning at the START index. If you don't specify any elements, Spice will only remove elements from the array. All right, let's go ahead and look at some examples will create an array called names and assigned to it the values of strings. John, Sarah, Eric and Jane. Let's use slice to not remove any elements, rather to go to index to and insert the value of the string tongue. Now, when we look at names, we have Tom at Index to instead of Eric. Now let's imagine that we want to cut out some elements from our array will target the index of three, which is now Eric and will remove one element. Our names element is now John, Sarah, Tom and Jane. There is more that we can do with the spice method, but this gives us enough functionality to accomplish what we need for our algorithm. Let's head to the lab and take the challenge. All right now you have the method that can easily help you accomplish creating an algorithm that satisfies the requirements we asked for. Go ahead and pause the video and give it a shot. Or, like I mentioned last time, pause the video and come up with some pseudo code for how you might think of solving the algorithm. Then look at the pseudo code I provide and then give a shot at solving the problem yourself . All right, let's pseudo code this. We're going to declare a function will pass to it. Two parameters, one for the array that were passing as an argument. The other parameter is the number of elements you want to spice off the head. Well, then go into our code block and attach the spice method to our array because we want to return our array. That has been changed, so we won't use the slice method that creates a new array. We need to pass to the spice method to arguments. The first is the starting point, and the second is how many elements to remove. Remember, the number of elements that you want to remove is the argument you're passing when you call the function. All right, go ahead and pause the video and give it a shot. It is only a few steps. It should be just a matter of putting the right info in the right spot. Even if you struggle with this. It's OK through practicing, you'll learn as much from when things don't work as when they do work will come back together in a few moments and work through a possible solution. All right, let's go ahead and declare A function called Chopper will give it the parameter of our for the array argument and will pass to it excess for the number of elements we would like to remove. We will then apply the spice method to our ray. We will pass that array to arguments. The first is the starting point because we want to cut off elements from the front of our array will always start it index zero that will begin with the first element in our array. Our second argument is going to be the excess parameter that we will be fed as an argument . When we call. Our function will then return our array that has been mutated. So let's call our chopper function will pass to it an array with elements consisting of strings listing the months of the year. We can see that two of those months aren't quite right, and we want to remove them. This array is going to be passed into our chopper parameter as the array. Our second argument in our function call is to this will represent the excess parameter in our chopper function. And in our co block will be the excess argument of spice cutting off two elements from the zero index. And the result is our normal months of the year. That's all. For now. Thanks for joining me and creating an algorithm to chop off elements from the front of Honore. We'll see you in our next lesson with our next question. 7. JavaScript the Basics - Functional Programming: Palindrome: All right, welcome back to Java script. The basics are challenge. This lesson involves palindromes. Ah, palindrome is a word that is spelled the same forwards and backwards examples would be a race car, dad and level our palindrome can be case and sensitive. We don't care if string characters are upper or lower case and considering a match. We also don't mind any non alphabetical characters. Such a spaces Comus, Ford slashes, hyphens underscores and parentheses. They are all okay. Race car with the dash would still be considered a palindrome. We're going to work through this challenge. Will use the whiteboard intra session to learn about those to then go into the lab and solve some algorithm challenges. Let's first take a refresher on regular expressions. What is a regular expression? A regular expression is a sequence of characters that form a search pattern. When you search for data in a text, you can use this search pattern to describe what you're searching for. A regular expression can be a single character or a more complicated pattern. Regular expressions can be used to perform all types of search, text and text. Replace operations. Let's take a refresher look at some syntax. First we have the pattern we're trying to match. Then we have the modifier. Here is some sample code. We have the Ford slash milk forward slash I. This is our regular expression. Milk is the pattern to be used in the search. We want to use regular expressions to deal with anything odd in our string like commas, periods underscores and so on. So we need a regular expression to match any of those characters that fit that pattern. The capital back slash W will catch all of these. It matches any non word character. We also need to use the global modifier G. Otherwise, we would Onley capture the first match. The other tool you're going to need to use is the method. Replace on strength. The replace method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a regular expression, and the replacement can be a string or a function to be called for each match. Let's look at a couple examples. Here is the rejects pattern. Let's look at the syntax. We could have a regular expression in which the match is replaced by the return value of the second parameter. Or instead of the Reg exe pattern, we could just use a strength. Let's do a quick example of the replace method on a string with both a normal sub string and a reg. Exe. We'll go ahead and create a string called Drink More Milk and a sign it's the variable milk ad will then create a variable called milk ault for milk. Alternative will assign it the value of the replace method on our milk ad variable for argument will pass the sub string milk that we want to replace with a new sub string silk that we want to replace it with. Now milk Olt returns. Drink more silk. Okay, let's do this one more time with the Reg. Exe we'll have our milk ad variable with the value drink. More milk will then create a new variable called milk sub and a sign it. The value of our milk ad, with the replace method attached will pass to the method. The arguments of the regular expression milk and our new sub string of soy milk milk sub now has the value of drink more soy milk. All right, you now have the basic tools you'll need to solve this algorithm challenge. Let's head into the lab and give it a shot. As always, Go ahead and pause the video and write out some pseudo code of how you might go about looking for a solution. Then all go ahead and walk you through my pseudo code. We're going to declare a function to check for whether a string is a palindrome. We're going to give it a parameter that we will feed our string argument that will lead us into the code block. We're going to want to get rid of any characters that aren't a through Z in the alphabet. So we use a regular expression, pattern and modifier to catch them all, and we will want to replace them with a string value that will essentially delete them so that we can get a fair and equal comparison. We will then use a method to make sure that all of the letters air upper or lower case because our palindrome can be case insensitive. After we have done that, we want to ask what the value is when we compare our string when we compare our string, we were given as an argument whether it is equal to the reverse of that string. You can reverse this string with the split method, reverse method and join method if you need a little extra clue. So now we just call our function and pass it a string as an argument. All right, go ahead and pause the video and give it a shot. Will come back together in a few moments to come up with a possible solution. All right, how to go. Let's come up with one possible solution to this challenge will declare a function called palindrome. We'll give it a parameter of str for whatever string argument we will wind up passing to it . We're now going to clean up our strength. We're going to set string equal to the string argument we're past will use the global back slash w to match any of the non A through Z characters. Those matches will be replaced with an empty string. Literal, which essentially is like a delete button, will then use the to lower case method on our string to make our palindrome case and sensitive, no matter what the string argument ISS. We also could have used to upper case if we would have liked to as well. Now we're going to want to return the value of our cleaned up string and a reverse to string. If they're equal, then we have a palindrome. We have done this before in a previous question. Will use the split method to turn our string into an array of sub strings of individual characters. Will then do the reverse method on the array to reverse the order, then the joint method on the array to turn the sub strings in that array back into a strength. We then return that value. At this point, we invoke our function. When we passed to it a string. We clean it up with the replace and Reg exe and then put in the lower case form. We then ask if are cleaned up. String is equal to our reversed string. Using split, reverse and joint, the value returned tells you whether the string is a palindrome or not. All right, thanks for joining me in this algorithm. Determining whether or not a string is a palindrome. We'll see you in our next lesson with our next question