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);
Related
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)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This function should swap the values in the array A that are at position i and j. For example, if A = [3,2,1,6,4]
swap(A, 2,3) should change A so it is now:
A = [3,2,6,1,4]
we're swapping the 1 at position 2 and the 6 at position 3 so now the 6 is at position 2 and the 1 is at position 3.
Problem is, I do not know how to swap these! (New to programming)
function swap(A, i, j) {
var swap = myArray[j];
myArray[j] = myArray[i];
myArray[i] = swap;
}
const A = [3, 2, 1, 6, 4];
console.log(swap(A, 2, 3))
Just need to pass the array and swapping indexes. You need to get the value at index i and j using array index as myArray[i] or myArray[j]
function swap(myArray, i, j) {
var swap = myArray[j];
myArray[j] = myArray[i];
myArray[i] = swap;
}
const array = [1, 2, 3, 4, 5, 6];
swap(array, 1, 3);
console.log(array)
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 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 5 years ago.
Improve this question
I have two arrays as below:
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
I want to compare arr2 with arr1, when it will find arr2 exactly in the same sequence as it is then it should return true. i.e. if [8,7,5] is found exactly same sequence in arr1 then it will return true.
Note:
We have to do this without using indexOf.
You could use a combination of Array#some and Array#every.
var array1 = [1, 2, 3, 8, 7, 5, 7, 2, 9, 0],
array2 = [8, 7, 5],
result = array1.some(function (a, i, aa) {
return array2.every(function (b, j) {
return aa[i + j] === b;
});
});
console.log(result);
You can loop through the largest array. On each iteration, compare the next values to all of the values found in the smaller array. If they all match, then it contains the smaller array.
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
console.log(doesArrayContain(arr2, arr1));
function doesArrayContain(smallestArray, biggestArray) {
for (var i = 0; i < biggestArray.length; i++) {
var doesMatch = true;
for (var j = 0; j < smallestArray.length; j++) {
if (biggestArray[i + j] !== smallestArray[j]) {
doesMatch = false; break;
}
}
if (doesMatch) {
return true;
}
}
return false;
}
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>