Calculating array in javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
sorry i new learning about javascript:
i have code like this:
function call(arr) {
//i have no idea
}
console.log(call([1,2,3]));
i want to output:
//[6,3,2]
where result output obtained from [(2*3),(1*3),(1*2)]

It looks like you want each value in the array to be replaced with the value of all of the other elements multiplied together. You can calculate this more efficiently by first multiplying all of the numbers together, and then using division to take away just one number from the product at any one time, leaving the product of all the other numbers in the array.
Array.prototype.reduce
Array.prototype.map
function call(arr) {
var product = arr.reduce((a,b) => a * b, 1);
return arr.map(num => product / num);
}
console.log(call([1, 2, 3]));
Note that this won't work if a 0 appears anywhere in the array.

Related

Does one push everytime creates array inside of array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I created button that is generate a random number every time , but in the console log it shows it like this:
small project i have been working on during course of javascript
I tried to make one array of random numbers and when it reach above 4 random numbers i will use Shift method to erase one. ( I also tried to use for loop )
What actually happen that is it kind of creates me new array inside of the orginal array
let randomArr = [];
if (randomArr.length >= 4) {
randomArr.shift();
lastNumbers.textContent -= randomArr + ",";
return randomArr;
} else {
randomArr.push(randomNumber);
// randomArr.length = randomArr;
lastNumbers.textContent += randomArr + ",";
// return randomArr;
}
Ty for helping!

Can map function in JS do multiple transformations at once? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I wonder whether a map-function in JavaScript can do more than one transformation to an array at once.
For instance, can the items of an array be transformed to be doubled AND then to be divided by 3?
Thank you
If you want to make multiple transformations to an array element at once all you have to do is use parenthesis to separate and have them be done in a certain order.
For your example, you could do it like this:
var array = [1, 2, 3, 4];
array.map(num => (num * 2) / 3);
Which would give you
[0.6666666666666666, 1.3333333333333333, 2, 2.6666666666666665]

JavaScript : How create proportionality rule function with array parameter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to create an Array from another by applying the proportionality rule.
For example:
Array1 = [15,550,76,3,400,230]
I take the maximum value from Array1 which is equal in my array 550
Now, with a function, I want to create a second array Array2 with a value equal to 10 which corresponds to 550 in the first array, like Array2 = [?,10,?,?,?,?]
I want to apply the proportionality rule function to determine the other values of Array2 to get Array2=[0.27,10,1.38,0.05,7.27,4.6]
Any help please how can I create the function with array param?
Thank you.
const array1 = [15,550,76,3,400,230]
function proportionalityRule(array){
const maxValue = Math.max(...array)
return array.map(element => (element/maxValue) * 10)
}
proportionalityRule(array1)
// (6) [0.2727272727272727, 10, 1.3818181818181818, 0.05454545454545455, 7.272727272727273, 4.181818181818182]

JavaScript - Compare two array and insert if not exists [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two arrays. For example:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
The output of second array should be,
[0,0,"Q5","Q6","Q7",0];
I want to compare the first array with the second and fill the missing values with Zero 0.
array1 is the primary array and, for now, array2 will have this 3 values initially.
Given the example arrays a simple map -> includes would work:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
console.log(array1.map(i => array2.includes(i) ? i : 0))

Remove non-numeric items in a listu using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)

Categories

Resources