Trying to understand Basic Algorithm Scripting: Confirm the Ending Solution

In the Basic Algorithm Scripting: Confirm the Ending exercise, the slice() method is used to solve the problem. Here’s where I get lost with this challenge. I would never think to use the slice() method in this way because both in the FCC lesson and Mozzila web docs list parameters as the only option.

Can we replace all javascript methods’ parameters w/ any math operation?
How do you know when to code something else verses following the syntax rules?

String.prototype.slice()

SyntaxSection

str.slice(beginIndex[, endIndex])

Basic Data Structures: Copy Array Items Using slice()

FCC slice()(https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/copy-array-items-using-slice)

let weatherConditions = [‘rain’, ‘snow’, ‘sleet’, ‘hail’, ‘clear’];

let todaysWeather = weatherConditions.slice(1, 3);

It looks like you’ve linked to the String prototype slice method, rather than the Array prototype slice method. As you can see on this one, the begin and end parameters are both optional.

In this case I think FCC was just telling you why, when, and how to use slice to copy an array without modifying the original, so don’t worry about not being able to come up with that by yourself. You just need to throw the knowledge into your toolbelt.

1 Like

So I was using the wrong prototype method in the first place, gotcha. I still get lost :confused: to know when to treat a problem as a string or array because that dictate what methods I should be using. Hopefully, one-day arrays won’t throw me for a loop before I get old and gray :older_woman:t5::joy:

Thanks, @colinthornton!