0
   

how do I multiply two arrays in JS?

 
 
PSilvan
 
Reply Wed 29 Oct, 2014 02:46 pm
Hi,

I want to multiply two arrays, element by element:

[1,2,3] * [4,5,6] = [4,10,18]

I am new to JS, I just have no clue... This is the furthest I got:


Code:var array1 = [1,2,3]
var array2 = [4,5,6]

function multiply() {
var mult = array1.pop()*array2.pop()
post("mult",mult)
post()
}


So, every time I execute multiply it multiplies the next two elements of each array. But not all at once, creating a new array.

Thanks,

PS


  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 2,944 • Replies: 1
No top replies

 
gungasnake
 
  1  
Reply Wed 29 Oct, 2014 05:31 pm
@PSilvan,
JS is meant as a control language for web pages. Even if you managed to get code to work for something like that, nobody would ever understand it or be able to modify it. Better would be to use Python or some real programming language to multiply the two arrays and JS to display the result, that would at least be guaranteed to work and be maintainable.
0 Replies
 
 

Related Topics

 
  1. Forums
  2. » how do I multiply two arrays in JS?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/26/2024 at 06:26:10