Functional Programming - Remove Elements from an Array Using slice Instead of splice

Tell us what’s happening:
Describe your issue in detail here.

Your code so far?
can someone please what line 8 and 9 are for?

function nonMutatingSplice(cities) {
  // Only change code below this line

return cities.slice(0, 3);
  // Only change code above this line
}

const inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
 nonMutatingSplice(inputCities);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Functional Programming - Remove Elements from an Array Using slice Instead of splice

Link to the challenge:

Ok. Thank You.
2nd question … Why is (cities) used for the functions argument when inputCities is the name of the array?

The ‘cities’ is the name of the function’s argument, which is defined at the time of the function declaration. You can pass any variable, even another function as an argument to that function, and it actually “becomes” ‘cities’ for that particular case.

I understand that cities is the name of the functions argument.
I also understand that you can pass a function on to another functions parameter.
I don’t understand how cities knows it’s an array named inputCities. and that it will .slice this array named something different.
I’m used to seeing an array and then having to slice or splice it… this lessons syntax is confusing me.

The function definition doesn’t know this at all. The function should not care about any global variables at all.

The function call identifies which specific values the function parameters will use for the current execution of the logic inside of the function.

1 Like

The ‘cities’ doesn’t know anything. It is some named argument and may be named as you wish, as long as the statement in the function uses that argument to produce, or return something:


As you can see here, I have changed the names of the argument and the variable, and the result is the same.

2 Likes

Thank You very much Jeremy! I get it!
I really hope other students are getting something from these questions. It’s really helping me!

1 Like

.this really helped me understand the lesson!!!

OUTSTANDING example! Thank You!

line 4 is simple now

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.