123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|426|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Javascript?

Mon, 05 Dec 2022, 14:26
dna

Javascript?


This is probably a stupid question but is there an algorithm for the map(x,y, . . ) command for the basic language?

-=-=-
DNA
Mon, 05 Dec 2022, 15:30
Jayenkai
How do you mean?
Wed, 07 Dec 2022, 11:29
dna
I mean what does this function do?
What is the equivalent algorithm in BASIC?

-=-=-
DNA
Wed, 07 Dec 2022, 11:35
Dan
Maybe this could help ? https://www.w3schools.com/jsref/jsref_map.asp
Wed, 07 Dec 2022, 13:43
dna
Is this the same or is there something else going on underneath?



-=-=-
DNA
Wed, 07 Dec 2022, 14:18
Dan
I do not know much about javascript, but looking on the w3schools page, a new array is returned by the function, containing the changed data.

something like this:

Wed, 07 Dec 2022, 14:57
dna
The examples in the link you provided show that it returns new values that replace the old ones in the original array unless I'm reading it wrong.
The second example is confusing even though its the same idea.

-=-=-
DNA
Wed, 07 Dec 2022, 15:25
Dan

Definition and Usage
map() creates a new array from calling a function for every array element.


It basically copies the array and performs whatever you want in the function.
The 1st example performs a Square root on each element. The Second example calls a function which performs *10 on each element.

You can test it with this code:


The original array is not changed.
Wed, 07 Dec 2022, 15:36
Jayenkai
In Javascript, think of "Map" as a function caller.
Rather than thinking of Map as a function itself, think of it as being a middle-bit, between the Array name and the Function

In plain english, "MyArray.map(SquareRoot)" would be like the phrase "For everything in MyArray, perform the command SquareRoot"

In most BASICs, that middle-bit command doesn't exist, and you have to code it.
This is what Dan's code does..

Rather than make it a function, you simply loop through everything in the array, and for each one, perform the actual required command.

For n=0 to LengthofMyArray : NewArray(n)=SquareRoot(MyArray(n)) : Next

-=-=-
''Load, Next List!''