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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need the sum total of the object, where, but the maximum of each similar pair
javascript
var score = {
"work_1": 5,
"work_1|pm": 10,
"work_2": 8,
"work_2|pm": 7
"work_3": 10
};
the work_3 doesn't have a pair similar
I wish this result
total = 28
Group the maximums by key-prefix in a new object, and then sum the values of that object. In both phases you can use reduce:
var score = {
"work_1": 5,
"work_1|pm": 10,
"work_2": 8,
"work_2|pm": 7,
"work_3": 10
};
var result = Object.values(
Object.entries(score).reduce((acc, [k, v]) => {
k = k.split("|")[0];
acc[k] = Math.max(v, acc[k] ?? v);
return acc;
}, {})
).reduce((a, b) => a + b, 0);
console.log(result);
You can get the property values of the object with Object.values, sort the resulting array, then sum the first 2 items:
var score = {
"work_1": 5,
"work_1|pm": 10,
"work_2": 8,
"work_2|pm": 7,
"work_3": 10
};
const max2 = Object.values(score).sort().splice(0, 2)
const result = max2[0] + max2[1];
console.log(result)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have string like
var t = 'red: 5, purple: 7, fuchsia: 10, green: 8';
I want to make array like
a = ['red', 'purple', 'fuchsia', 'green'];
b = [ 5, 7, 10, 8]
Please help me
Sample solution:
const t = 'red: 5, purple: 7, fuchsia: 10, green: 8';
const tarr = t.split(', ');
const a = [];
const b = [];
for (const item of tarr) {
const [k, v] = item.split(': ');
a.push(k);
b.push(~~v); // '~~' is a shortcut to convert string value to number
}
console.log(a, b);
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 years ago.
Improve this question
x = [1, 2, 3, 4]
t = []
c = []
for(i = 0; i<x.length; i++){
function solution(){
t = Math.pow(x, 2)
c = t[i]++
return c
}
}
The function needs to:
Count the squares of the array numbers
Then to get sum of squared (I am not sure if I wrote it correct, LOL) numbers. Thanks!
const result = [1, 2, 3, 4].map(n => n ** 2).reduce((acc, val) => acc + val, 0);
console.log(result);
You can also use ** operator which is faster than Math.pow(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
var result = [1, 2, 3, 4].map(n => Math.pow(n, 2)).reduce((acc, val) => acc + val, 0);
console.log(result);
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 have a very interesting Problem and would like to hear some approaches that you would take.
Scenario:
A Tournament of 32 Players, each represented in an Array of Objects ex:
[
{ player: 'Badgy', points: 5, place: tba, reward: 0 },
{ player: 'Ceff', points: 5, place: tba, reward: 0},
{ player: 'Niclas', points: 10, place: tba, reward: 0}
]
Now there are prices defined for each of the top places like:
1 Place = 100 Coins
2 Place = 50 Coins
3 Place = 10 Coins
Now in this example 'Ceff' and 'Badgy' have the same point amount, which means they both have to be place 2 and get the reward of (place2 + place3) / 2, each of them would get 30 coins in this example.
Now I tried around but I have a hard time finding a good solution to this case, specially if a 3+ way tie happens.
You could take a sum and an index for the first found group of same points and take the average for the last same group.
var prices = [100, 50, 20, 15, 10, 5, 2, 1],
points = [ 20, 10, 10, 5, 5, 5, 4, 3],
sum = 0,
index,
profit = points.reduce((r, v, i, { [i - 1]: last }) => {
if (last !== v) {
sum = 0;
index = i;
}
sum += prices[i];
var avg = sum / (i - index + 1);
while (i >= index) r[i--] = avg;
return r;
}, []);
console.log(profit);
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 4 years ago.
Improve this question
Input
Array =[4,2,8,11,14,6,1]
Expectedtotal=20
Output
[2,4,6,8], [11,8,1],[6,11,1,2],[2,14,4],[14,6]
code:
const arr = [4, 2, 8, 11, 14, 6, 1];
const target = 20;
res = _.filter(arr, v => _.filter(arr, v1 => _.filter(arr, v2 => _.filter(arr, v3 => v + v1 + v2 + v3 === target))); console.log(res);
I am the beginner of javascript and lodash. I tried this code by own..please help me to write good code.
You could use a temporary array and the index and iterate the next index by either taking the actual value or not.
function subsetSum(array, sum) {
function iter(index, temp, s) {
if (s === sum) return result.push(temp.slice()); // exit sum found
if (index >= array.length) return; // exit index over
iter(index + 1, temp.concat(array[index]), s + array[index]); // take value
iter(index + 1, temp, s); // omit value
}
var result = [];
iter(0, [], 0);
return result;
}
console.log(subsetSum([4, 2, 8, 11, 14, 6, 1], 20).map(a => a.join(' ')));
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 7 years ago.
Improve this question
var array = [0, 1, 2, 3];
var numbers = [3, 6 ,2, 7];
I want to multiply each number in array with numbers without repetition of numbers from numbers
Fixed number for all index
var array = [0, 1, 2, 3];
array.map(
function(n){
return (n* number to be multiplied);
}
);
Different number for each index
var array = [0, 1, 2, 3], numberToBeMultiplied = [1,3,5,7];
array.map(
function(n, i){
return n * numberToBeMultiplied[i];
});
You can also push the returning elements in an array.
Your question is not clear enough, what I get you may want to do something like this.
var array = [1,2,3,4]
var array2 = [];
for(var i = 0; i < array.length; i++) {
var randomMultiplier = Math.floor((Math.random() * 10) + 1); //Return a random number between 1 and 10
array2.push(array[i] * randomMultiplier);
$('#multiplied-list').append('<li>'+array[i] * randomMultiplier+'</li>')
}
console.log(array2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="multiplied-list"></ul>