I want to get the common multiple in any given array, the problem i think is in my loop condition i dont want j to be less than 30, i want j to keep multiplying until the common multiple is found you can see the desired output down, how can i do this? thanks in advance.
function m(arr){
for(let i = 0; i < arr.length; i++){
console.log(`when arr[i] = `, i);
for(let j = arr[arr.length - 1]; j < 30; j += arr[arr.length - 1]) {
if(j % arr[i] === 0) console.log(j)
}
}
}
m([2,3,4,5])
this code outputs:
when arr[i] = 2
10
20
when arr[i] = 3
15
when arr[i] = 4
20
when arr[i] = 5
5
10
15
20
25
desired output
when arr[i] = 2
60
when arr[i] = 3
60
when arr[i] = 4
60
when arr[i] = 5
60
According to #Prosy Arceno's suggestion extended a little.
Thanks Prosy.
function m(arr) {
let m = multiply( arr )
for (let i = 0; i < arr.length; i++) {
console.log(`when arr[i] = `, i);
for (let j = arr[ arr.length - 1 ] ; j < m ; j++) {
let result = arr.every(function (element) {
return j % element === 0;
});
if (result) { console.log(j); break }
}
}
}
multiply =( arr )=>{
let m = 1
arr.map( element =>{ m = m * element } )
return m
}
m([2, 3, 4, 5 , 18]);
I think that your general approach is a bit problematic. Your inner for loop searches for the first common multiple of your current arr[i] and the last element of the array arr[arr.length-1].
In your example, this boils down to the inner loop logging a 10 because it is the first common multiple of 2 and 5.
There are many ways to solve your problem (see https://en.wikipedia.org/wiki/Least_common_multiple)
You may use every method of Array.
function m(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(`when arr[i] = `, i);
for (let j = arr[ arr.length - 1 ] ; j < 100; j++) {
let result = arr.every(function (element) {
return j % element === 0;
});
if (result) { console.log(j); }
}
}
}
m([2, 3, 4, 5]);
function m(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(`when arr[i] = `, i);
for (let j = 5; j < 100; j++) {
let result = arr.every(function (element) {
return j % element === 0;
});
if (result) console.log(j);
}
}
}
m([2, 3, 4, 5]);
Related
Yes, as the title suggests
Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices.
What I have done so far
function pairwise(arr, arg) {
if (arr.length === 0) return 0
let res = [];
let indexes = [];
let indexArr = []
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arg === arr[i] + arr[j] && !indexes.includes(i) && !indexes.includes(j)) {
res.push([arr[i], arr[j]]);
indexes.push(i);
indexes.push(j);
}
}
}
console.log(res)
for (let i = 0; i < res.length; i++) {
for (let j = 0; j < res[i].length; j++) {
indexArr.push(arr.indexOf(res[i][j]))
}
}
return indexArr.reduce((curr, prev) => curr + prev)
}
pairwise([1, 1, 1], 2);
I am doing this in Freecodecamp. It passes some test but fails the following tests:
pairwise([1, 1, 1], 2) should return 1. (above code returns 0)
pairwise([0, 0, 0, 0, 1, 1], 1) should return 10. (above code returns 8)
I think I am doing wrong in the indexOf part. How to solve this?
EDITED:
Can someone help me with the problem below further. I have a class and an array inside the class. I want now use a for loop to sum the length of the previous array length, but for each iteration. If i == 1 I want sum the length of arr[0].x.length, If i == 2 I want sum the length of arr[0].x.length+arr[1].x.length, ect. It will be a lot of code to check each iteration.
Is there a simple way to do this? Instead allways use a new line like
if (i == 1) n = n + arr[i-1].x.length;
if (i == 2) n = n + arr[i-1].x.length+arr[i-2].x.length;
if (i == 3) n = n + arr[i-1].x.length+arr[i-2].x.length+arr[i-3].x.length;
function Class() {
var x = [];
}
for (var i = 0; i < 9; i++) {
arr[i] = new Class();
}
I add 4 items to each object.
arr[0].x.push(...)
arr[0].x.push(...)
...
arr[1].x.push(...)
arr[1].x.push(...)
...
var n = 0;
for (var i = 0; i < arr.length; i++) {
if (i == 1) {
n = n + arr[i-1].x.length;
} else if (i == 2) {
n = n + arr[i-1].x.length+arr[i-2].x.length;
} else if (i == 3) {
n = n + arr[i-1].x.length+arr[i-2].x.length+arr[i-3].x.length;
}
// ect.
}
You could use reduce to get a total of all the lengths of your sub-arrays. For example:
const arrs = [[1, 2, 3], [4, 5, 6]];
const sum = arrs.reduce((acc, arr) => acc += arr.length, 0);
console.log(sum);
// 6
Just nest the loop two times: go over the indexes once then go up to that index from 0 in an inner loop:
for (var i = 0; i < arr1.length; i++) {
for(var j = 0; j <= i; j++) {
n = n + arr1[j].length;
}
}
Edit: benvc's answer is what you are looking for if you want to use reduce.
var arr = [[1,2,3], [4,5,6], [7]];
var n = 0;
for (var i = 0; i < arr.length; i++){
n += arr[i].length;
}
console.log(n);
So basically there are two sequences from I to j and j to k.
For example 3 to 5 and 5 to 2.
And we need to know the sum. 3 + 4 + 5 + 4 + 3 + 2.
And my code is not working.
var arr = [];
var sum = 0;
function pushIn(i, j, k){
for(var a = i; a < j; a++){
arr.push(a);
}
for(var a = j; a == k; a--){
arr.push(a);
}
for(var i = 0; i <arr.length;i++){
sum += arr[i];
}
}
}
I think the problem lies in your second for loop
Perhaps you should try this
for(var a = i; a < j; a++){
arr.push(a);
}
for(var a = j; a > k; a--){
arr.push(a);
}
for(var i = 0; i <arr.length;i++){
sum += arr[i];
}
Hope this helps
In for loop, second argument is a comparator- condition for executing the code block.
In your second loop, condition is never met hence it does not iterate at all.
In your case, I am assuming you want loop to be iterated unless it is less than or equals to k hence you need to make it >= so that condition will meet andloop will be iterated.
var arr = [];
var sum = 0;
function pushIn(i, j, k) {
for (var a = i; a < j; a++) {
arr.push(a);
}
for (var a = j; a >= k; a--) {
arr.push(a);
}
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
console.log(pushIn(3, 5, 2));
You may use .concat() and .reduce() to get the resultant value:
let reducer = (i, j, k) => [].concat(
Array.from({length: j - (i - 1)}, (_, index) => i + index),
Array.from({length: j - k}, (_, index) => j - (index + 1))
).reduce((r, c) => r + c, 0);
console.log(reducer(3, 5, 2));
I come from a Ruby background, which features an enumerable class. In Ruby, I can easily find combinations of array elements.
array.combination(2).count
I know that JavaScript doesn't feature such built in functions, so I was wondering how I could implement this in JS. I was thinking something like
I have an array as follows
var numbers = [9,7,12]
var combos = []
for (var i = 0; i < numbers.length; i++) {
combos.push([numbers[i], numbers[i+1])
}
By the way, the possible combos are
[9,7], [9,12] and [7,12]
so by calling the length function on this array, 3 would be returned.
Any ideas?
How about:
for (var i = 0; i < numbers.length; i++)
for (var j = i + 1; j < numbers.length; j++)
combos.push([numbers[i], numbers[j]]);
Are you strictly talking about 2-combinations of the array or are you interested in a k-combinations solution?
Found this in this gist
function k_combinations(set, k) {
var i, j, combs, head, tailcombs;
if (k > set.length || k <= 0) {
return [];
}
if (k == set.length) {
return [set];
}
if (k == 1) {
combs = [];
for (i = 0; i < set.length; i++) {
combs.push([set[i]]);
}
return combs;
}
// Assert {1 < k < set.length}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
head = set.slice(i, i+1);
tailcombs = k_combinations(set.slice(i + 1), k - 1);
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
Here's a recursive function, which should work for any number:
function combination(arr, num) {
var r= [];
for(var i = 0 ; i < arr.length ; i++) {
if(num===1) r.push([arr[i]]);
else {
combination(arr.slice(i+1), num-1).forEach(function(val) {
r.push([].concat(arr[i], val));
});
}
}
return r;
} //combination
Working Fiddle
I have an array with N objects
var test = [obj1, objc2, ..., objN]
The object is in this format
{"Key", "number", "Name" : "string", "OtherFields" : "Data"}
I want to iterate like this
for (var i = 0; i < test.length; i++) {
for (var j = 1; j < test.length; j++) {
for (var k = 2; k < test.length; k++) {
console.log(test[i].Name + test[j].Name + test[k].Name)
}
}
}
How can i achieve this result for n objects in the array ?
Example:
I have arrays [1, 2, 3, 4] , [2,4,5], [1,2]
i want to get this result:
1 2 1
1 2 2
1 4 1
1 4 2
1 5 1
1 5 2
.....
.....
4 5 2
Those arrays are not always 3 but can be any number >= 2
If I understand correctly, you want permutations.
Here is a recursive approach:
function perm(test, max_depth, depth, s){
if(depth === undefined)
depth = max_depth;
if(s === undefined)
s = '';
if(depth == 0)
console.log(s);
for(var i=depth; i<test.length;i++){
perm(test, max_depth, depth-1, s + test[i].Name);
}
}
var N = 10;
var n = 3;
var test = [];
for (var i = 0; i < N; i++) {
test.push({"Name" : "obj" + i});
}
function log_name_combinations(a, n) {
function nestedlog(a, maxlvl, lvl, s) {
if (lvl < maxlvl) {
for (var i = lvl; i < a.length; i++) {
nestedlog(a, maxlvl, lvl+1, s + a[i].Name);
}
} else {
console.log(s);
}
}
nestedlog(a, n, 0, "");
}
log_name_combinations(test, n);
Demo at JsFiddle
PS: Why are you starting the ith level iteration at the ith index?