What is the best method for multiplying two arrays of numbers together using JavaScript?

I have been attempting to create a function that can multiply two arrays of numbers together without using array.join("") * array2.join("").

I have experimented with various methods, such as:

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

However, this method only works for multiplying each number in the array by a single scalar value.

I am seeking a function that can achieve the following:

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 take note: I do not want the solution to involve something like

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

I am willing to offer all my reputation points as a bounty to anyone who can provide a satisfactory answer to my query.

Answer №1

If you're facing issues with scientific notation, consider converting the arrays into BigInts.

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));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining a JSON object from a URL through 'GET' request using $resource in AngularJS

Just starting out with AngularJS and struggling to understand web services as well. My current task involves fetching a JSON object from the following URL: To retrieve the JSON object, I need to access: I am completely lost on how to proceed... Any assis ...

Using radio buttons to toggle the visibility of a div element within a WordPress website

I am currently working on creating a WordPress page using the custom page tool in the admin interface. My goal is to have 3 radio buttons, with 2 visible and 1 hidden. The hidden button should be automatically checked to display the correct div (although ...

Here's the step-by-step process: Access the specific item in the object by referencing `obj[i]["name of desired attribute"]

I tried seeking advice and consulting multiple sources but none provided a suitable answer. Is there someone out there who can assist me? obj[i].["name of thing in object"] Here's the array: [ { "name": "DISBOARD#2760" ...

Is there a way to instantly remove script from the document head using jQuery instead of waiting a few seconds?

I currently have a setup where I am utilizing Google Maps in production. To make this work, I must include a script in the head of my document that contains the API key for the Google Maps JavaScript API that my application relies on. The API key is being ...

Is it possible to utilize a JavaScript framework within a Chrome extension?

Currently, I am developing a chrome extension that adds a toolbar to the DOM dynamically. In order to find, attach, and manipulate elements, I have been using CSS selectors in JavaScript. However, this approach has proven to be fragile as any changes made ...

AngularJS not passing date data to web API

Greetings! I am currently working on a web application using AngularJS. I have a date value in AngularJS, for example 13-10-2017. In C#, I have the following field: public DateTime LicenseExpiryDate { get; set; } When I send 13-10-2017 in an AJAX reques ...

What is the most effective method for embedding a Kotlin program into a website?

I have created a combat simulation tool in Kotlin for an online gaming community. Users can input the combat levels of two players, choose the number of duels to simulate, and then initiate the simulation which will provide win percentages and other stats. ...

The panel widens unexpectedly despite the initial coding plan

I am in the process of creating a panel with a specific area where users can resize it by dragging their mouse cursor. However, instead of moving smoothly, the panel unexpectedly jumps in size before becoming resizable. Upon inspecting the element, I noti ...

Issue with fetching access token from Azure /oauth2/token endpoint using jQuery, Ajax, or AngularJS due to cross-origin restrictions

I am attempting to obtain an access_token from Azure. Error Message: Unable to fetch : No 'Access-Control-Allow-Origin' header is found on the requested resource. Origin 'http://localhost:61697' cannot access it. Code snippet: fun ...

Modify the text highlighted in bold within the AngularJS application

In my angular.js application, I have a table that displays elements. The name of these elements sometimes needs to be displayed in bold by adding <b> and </b> tags. However, instead of rendering the name as HTML code, it is showing up as a stri ...

Is there a barrier I've reached with the amount of hashtags I'm using?

My goal is to limit the use of javascript and rely more on CSS to achieve desired effects on my website. One feature I have developed is a modal that opens using hashtags and targets, similar to this example: http://codepen.io/maccadb7/pen/nbHEg. While th ...

Is there a way to use lodash to convert an array into an object?

Below is an array that I am working with: const arr = [ 'type=A', 'day=45' ]; const trans = { 'type': 'A', 'day': 45 } I would appreciate it if you could suggest the simplest and most efficient method to ...

Why isn't setInterval set to a duration of one thousand milliseconds?

While trying to use some code from w3schools, I noticed that the setInterval in the example is set to 5 instead of 5000. Shouldn't it be in milliseconds? If not, how can I speed up this animation? When I try reducing it to decimals like 0.01, the anim ...

Occasionally, the view fails to update following an $http request

Although this question has been posed multiple times before, none of the solutions seem to be effective for my issue. Controller app.controller('HomeController', function ($scope, $timeout, $http) { $scope.eventData = { heading: ...

What is the best way to send an input response to a function in C?

In my custom "shell" program, I am asking users what actions they would like to take. The code snippet below showcases the preliminary code I have set up: /* Create a char array to store user response */ char response[80]; char exit[4] = "Exit"; prin ...

Traversing through Object consisting of dual arrays within VueJS

Currently, I am working on a chat application built in VueJS and encountering an issue when attempting to display messages along with their respective timestamps. The challenge arises from the need to iterate through an object that contains two arrays: one ...

Error in PromisifyAll: Callback parameter must be a function

Either my understanding of how BlueBird and its promisify function works is incorrect, or I am making a mistake in the following code. I have an "upload-handler" module that exports one function with a callback: The structure of the upload-handler functio ...

What is the correct method for implementing a To-Do List?

I am trying to create a to-do list, but I am facing an issue with using splice to remove a specific item e.target.item. It seems like it would be easier if I were using Angular 5. Can anyone assist me with this problem? Thank you. import React, { Compone ...

What is the best way to invoke a function only once in typescript?

Struggling to implement TypeScript in React Native for fetching an API on screen load? I've been facing a tough time with it, especially when trying to call the function only once without using timeouts. Here's my current approach, but it's ...

Unable to change the color of InputBase component's placeholder in React.js

I've been attempting to modify the color of the placeholder in an inputbase. I came across several methods online and tried implementing them, but none have been successful. Below are the codes I have tried. <InputBase id="input-id&quo ...