3

I've been trying to make an array of numbers be able to times another array of numbers without doing array.join("") * array2.join("").

I've tried a lot of methods such as:

var input = [3, 6, 4];
var scalar = 5;
var output = input.map(x => x * scalar); // [15, 30, 20]

Although that's only one number the array can multiply to.

I'd like a function that can do:

var array = [ 1, 3, 2 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2 ];
someFunction(array, array2);
// [ 7, 1, 0, 4, 7, 0, 4, 6, 4 ]

Please note I don't want it to be something like

array.join("") * array2.join("")

I'm willing to give all my reputation as a bounty if someone is able to answer my question.

3
  • Turning them both into numbers, multiplying, then turning back into an array would be by far the simplest solution. Anything else would be pretty unnecessarily convoluted - is there a reason you don't want to go that route? There isn't a lot of use to re-inventing multiple digit multiplication IMO Commented Mar 1, 2022 at 2:09
  • Yes, if the array is over 20 digits, it gives scientific notation. Commented Mar 1, 2022 at 2:10
  • 1
    The answer depends a lot on why you want to do this. Is it an intellectual challenge? Then try actually implementing per digit multiplication. Is it production code? If so stop everything you are doing and reevaluate the choices that led to an array of digits. Commented Mar 1, 2022 at 2:45

1 Answer 1

3
+150

If scientific notation is the problem, turn the arrays into BigInts instead.

var array = [ 1, 3, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
const someFunction = (arr1, arr2) => [...String(
  BigInt(arr1.join('')) * BigInt(arr2.join(''))
)].map(Number);
console.log(someFunction(array, array2));

Sign up to request clarification or add additional context in comments.

6 Comments

I had no idea all of that could be squashed inside of 3 lines, it took me almost like 50+ lines and still couldn't get there, thank you so much. Made the answer simple and easy to understand.
Hi, was also wondering if this works on arrays? If I was to replace the * by a / would it still work or are there other changes that are needed? Thanks.
It can be used, but not to indefinite precision - without other modifications, you'll lose everything past the decimal place. You can compensate for that by first multiplying one of the numbers by 10n ** 100n (or whatever is appropriate) to increase the number of correctly calculated decimal digits by 100.
Hi, I found a minor maths error. E.g. If I were to times 900000000000000000000000*900000000000000000000000 in an array form, it'll return: [...String( BigInt(900000000000000000000000) * BigInt(900000000000000000000000))].map(Number) // returns [ 8, 0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 2, 4, 5, 0, 2, 5, 2, 8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 5, 9, 2, 1, 8, 6, 0, 4, 4, 4, 1, 6, 0, 0 ]
For large numbers, you must pass strings, not numbers, to BigInt. Do BigInt('90000..., not BigInt(90000... and then I think it works properly.
|

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.