I have a question about one parameter in the function below. I want to focus on parameter v. I have been trying to look for an answer but I could not figure out why there must be the parameter v.
On the MDN (Array.from() - JavaScript | MDN) I could only find that v will be undefined when I print it into the console after I already tried that and figured out this part. But there is written nothing about why this parameter must be there. I have tried to remove it and it broke the code so do you have an answer for why it must be there? Thank you very much.
Instructions to the code: Build Tower by the following given argument: number of floors (integer and always greater than 0).
PS: Code works as intended.
function towerBuilder(n) {
return Array.from({ length: n }, function (v, k) {
const spaces = " ".repeat(n - k - 1);
return spaces + "*".repeat(k + k + 1) + spaces;
});
}
The second argument to from is the same callback for map
The first argument to that is the current value. You can’t just arbitrarily not bother with an argument in JavaScript (or any other language unless it has named arguments) if you need to use the parameters after, so it has to be there. How else would you get the index?
That is also the reason why an option object is often used. So instead of relying on a fixed list of parameters, you pass an object and it has properties on it corresponding to what would otherwise have been parameters.
Ok I think I got it. I did not get the whole concept from your answers and therefore I tried to look for something on Youtube. I found this gem (Array.from Method in JavaScript - YouTube) that explains everything.
Thank you very much for your time and your answers.