Algebra
Mathematics
Algorithms
Logic
function multiply(a, b){ a = String(a).split('').reverse() b = String(b).split('').reverse() partial_sums = [] a.forEach((value, index)=>{ let r = b.map(othervalue=>{return value*othervalue}) partial_sums.push(Array(index).fill(0).concat(r)) }) result = Array(a.length + b.length - 1).fill(0) partial_sums.forEach(sum=>{ sum.forEach((digit,index)=>{ result[index] = result[index] + digit }) }) return result.reverse() }
def multiply(a, b):#your codepass- function multiply(a, b){
- a = String(a).split('').reverse()
- b = String(b).split('').reverse()
- partial_sums = []
- a.forEach((value, index)=>{
- let r = b.map(othervalue=>{return value*othervalue})
- partial_sums.push(Array(index).fill(0).concat(r))
- })
- result = Array(a.length + b.length - 1).fill(0)
- partial_sums.forEach(sum=>{
- sum.forEach((digit,index)=>{
- result[index] = result[index] + digit
- })
- })
- return result.reverse()
- }
Test.expect(multiply(123, 456), [4, 13, 28, 27, 18]); Test.expect(multiply(42, 424), [16, 16, 20, 8]);
Test.assert_equals(multiply(123, 456), [4, 13, 28, 27, 18]);Test.assert_equals(multiply(42, 424), [16, 16, 20, 8]);- Test.expect(multiply(123, 456), [4, 13, 28, 27, 18]);
- Test.expect(multiply(42, 424), [16, 16, 20, 8]);