Algorithm Solutions
I’ve completed three more Free Code Camp algorithms since my last go on May 30th.
I needed less guidance on these particular algorithms, all but one. The algorithm solutions weren’t too hard to come up with, however on Slasher FLick I really overcomplicated my solution. I was thinking way too hard about how to solve it— new array? Should I return newArray as a part of the function call? Do I push the result of the cut off part of the array into newArray?
The instructions were:
The head means the beginning of the array, or the zeroth index.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
Array.slice()
Array.splice() Seems simple enough. But I was thinking too much and making it more complex than it needed to be because of my recent previous solutions.
I used splice instead of slice as splice returns the chopped off part as a new array. For these instructions, here was the initial code:
[javascript]
function slasher(arr, howMany) {
// it doesn't always pay to be first
}
slasher(["burgers", "fries", "shake"], 1);
[/javascript]
Originally I had this:
I would get a double array, because like I said, splice returns a new array from the chopped off part. So I tried to use a non-initialized variable— var newArray;— that returned a TypeError.
I went to the Free Code Camp wiki to look at the explanation in more detail. I finally came up with an Aha! Moment. I only needed to return the array that was resulting in the splice method.
I settled on my final solution here:
Not too bad.
Mutations.
The instructions:
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"]should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
String.indexOf()
I thought, okay. I got this.
The code:
I started out with this:
That didn’t work. I went to Gitter.
A guy there told me I needed to compare two arrays. I couldn’t figure out what he meant by that. To the wiki.
There I found out I should use toLowerCase and think about turning the array strings into an array of chars.
So then I came up with this:
as part of the equation. That also didn’t work. By this time, I am tired, it is late and I just wanted this to work. I went back to the wiki and found the solution.
I wouldn’t have ever came up with this yesterday night:
I am studying this. I am trying to figure out what is going on here and I will probably go to Code Newbie Slack to ask around.
I’ve completed three more Free Code Camp algorithms since my last go on May 30th.
I needed less guidance on these particular algorithms, all but one. The algorithm solutions weren’t too hard to come up with, however on Slasher FLick I really overcomplicated my solution. I was thinking way too hard about how to solve it— new array? Should I return newArray as a part of the function call? Do I push the result of the cut off part of the array into newArray?
The instructions were:
The head means the beginning of the array, or the zeroth index.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
Array.slice()
Array.splice() Seems simple enough. But I was thinking too much and making it more complex than it needed to be because of my recent previous solutions.
I used splice instead of slice as splice returns the chopped off part as a new array. For these instructions, here was the initial code:
[javascript]
function slasher(arr, howMany) {
// it doesn't always pay to be first
}
slasher(["burgers", "fries", "shake"], 1);
[/javascript]
Originally I had this:
I would get a double array, because like I said, splice returns a new array from the chopped off part. So I tried to use a non-initialized variable— var newArray;— that returned a TypeError.
I went to the Free Code Camp wiki to look at the explanation in more detail. I finally came up with an Aha! Moment. I only needed to return the array that was resulting in the splice method.
I settled on my final solution here:
Not too bad.
Mutations.
The instructions:
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"]should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
String.indexOf()
I thought, okay. I got this.
The code:
I started out with this:
That didn’t work. I went to Gitter.
A guy there told me I needed to compare two arrays. I couldn’t figure out what he meant by that. To the wiki.
There I found out I should use toLowerCase and think about turning the array strings into an array of chars.
So then I came up with this:
as part of the equation. That also didn’t work. By this time, I am tired, it is late and I just wanted this to work. I went back to the wiki and found the solution.
I wouldn’t have ever came up with this yesterday night:
I am studying this. I am trying to figure out what is going on here and I will probably go to Code Newbie Slack to ask around.
I will also be reading all the algorithm books I have and the Coursera courses I downloaded from Stanford.
© tiff.RSS