Jump to content
Sign in to follow this  
hisoka

things I could not understand in a script

Recommended Posts

 I a  script in this page :

https://stackoverflow.com/questions/24094466/javascript-sum-two-arrays-in-single-iteration

this is the script :

var array1 = [1,2,3,4];
var array2 = [5,6,7,8];

var sum = array1.map(function (num, idx) {
  return num + array2[idx];
});

the above script adds elements of two arrays . However , I did not understand it .  I read about the map() function here :

https://www.w3schools.com/jsref/jsref_map.asp

Still I did not understand what it means by

Quote

The map() method creates a new array with the results of calling a function for every array element.

When I changed the num and idx with other values like for example numb and rx  , it says :

Quote

idx is not defined

Quote

num is not defined

but in this page https://www.w3schools.com/jsref/jsref_map.asp

num and idx are not mentioned as necessary parameters for the function I thought these parameters are normal like any other function parameters and therefore they can be named anything .

 

I do not understand what script does in the last two lines .

one last thing when I changed the PLUS value to XOR value , I got an incomplete result :
 

var array1 = [1,2,3,4];
var array2 = [5,6,7,8];

var sum = array1.map(function (num , idx ) {
  console.log(num ^ array2[idx]);
  return num ^ array2[idx];
 
});

I got 4 12 as a result instead of 4  4 4 12  why ?

 

 

 

 

Share this post


Link to post
Share on other sites

I thought these parameters are normal like any other function parameters and therefore they can be named anything .

They can.  If you want to change the name of a variable, change it in every place.  You changed them in one place but you're still trying to use the old names, so that's the error.

If you want to understand what map does, these 2 are functionally equivalent:

var ar2 = ar1.map(func);

var ar2 = [];
for (var i = 0; i < ar1.length; i++) {
  ar2[i] = func(ar1[i], i);
}

I got 4 12 as a result instead of 4  4 4 12  why ?

You're reading the output wrong.  If you print the array sum, you'll see it contains 4 elements.  Your console is probably only printing the 4 once but indicating somewhere on the right side that it was printed 3 times.

  • Thanks 1

Share this post


Link to post
Share on other sites

 

var ar2 = ar1.map(func);

 

var ar2 = [];
for (var i = 0; i < ar1.length; i++) {
  ar2[i] = func(ar1[i], i);
}

according to the above , and if we take this in consideration :

var array1 = [1,2,3,4];
var array2 = [5,6,7,8];

var sum = array1.map(function (num, idx) {
  return num + array2[idx];
}); //

I understand that the map function creates an empty array . as mentioned in this page https://www.w3schools.com/jsref/jsref_map.asp

then loop through every element in the array1 and add it to the first index element of the array2 until  all elements of arrays one and two are added   then each time assigns the  result to the new created array .

Is it so or I am wrong ?

Share this post


Link to post
Share on other sites

That's what that particular code does, yes.  If that's what you're going to do though, I would do it with a for loop so it's more obvious what you're doing.

var sum = [];
for (var i = 0; i < ar1.length; i++) {
  sum[i] = ar1[i] + ar2[i];
}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×