Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have two arrays:
Ex: count["1","3","1"..........], amount["10","20","30"..........]
count["1","3","1"..........]
amount["10","20","30"..........]
I need to perform multiplication & addition as:
var total = (1x10)+(2x20)+(1x30)...........
Can anyone help. Thanks
You can use this code
var count = [1, 3, 1]; var amount = [10, 20, 30]; var sum = 0; for (var i = 0; i < count.length; i++) { sum += count[i] * amount[i]; } console.log(sum)
Add a comment
var a = ["1","3","1"], b = ["10","20","30"], sum = 0; $.each(a, function(k,v){ sum += (parseInt(v) * parseInt(b[k])); }) console.log(sum); // 100
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
100
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.