comparing two arrays to create a new array [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 1 year ago.
Improve this question
This seems like a very straightforward problem but for the life of me, I can not get it sorted.
I want to compare two arrays and if the value of array2 is in array1 push it to a new array otherwise push a 0.
these are my arrays:
let arrayOne = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
let arrayTwo = [3, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16];
desired outcome is
[0,0,3,4,5,0,0,8,9,10,11,12,13,14,15,16,0,0]
current attempt with obvious indices issues
let newArray = [];
for (let i=0; i < arrayOne.length; i++){
if(arrayTwo.includes(i)) newArray.push(arrayTwo[i]);
else newArray.push(0);
};
current result
[0, 0, 0, 8, 9, 10, 0, 0, 13, 14, 15, 16, undefined, undefined, undefined, undefined, undefined, 0]

Issue with your approach is that you are searching for an index in arrayTwo
if(arrayTwo.includes(i)) newArray.push(arrayTwo[i]);
where i is the index not a value. That's why you are getting undefined values, because arrayTwo has less values then arrayOne, so index of arrayOne will not exists in arrayTwo.
Instead you should search for value in arrayTwo
for (let i=0; i < arrayOne.length; i++){
let value = arrayOne[i];
if(arrayTwo.includes(value)) newArray.push(value);
else newArray.push(0);
};
If you check only for existence of the value in collection - use Set which designed exactly for this purpose.
let arrayOne = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
let values = new Set([3, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16]);
const result = arrayOne.map(value => values.has(value) ? value : 0);
// => [0,0,3,4,5,0,0,8,9,10,11,12,13,14,15,16,0,0]

Related

how can I skip two numbers and then get the next three numbers? [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 8 months ago.
Improve this question
For a given range of numbers, how can I skip two numbers and then get the next three numbers?
e.g. For the range 0..20
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
for (let list = 0; list < 100; list++) {
console.log(list);
}
I need to get this type of result:
2, 3, 4, 7, 8, 9, 12, 13, 14....
Loop through from your start to the end. Use modulus arithmetic to determine which part of the 5 number cycle the number lies on. If it's the 0th, or 1st then ignore, otherwise add them to your result:
let start = 0;
let end = 20;
let result=[];
for(let i=0; i<(end-start); i++)
{
let mod = i % 5;
switch(mod)
{
case 0:
case 1:
// Ignore these numbers
break
case 2:
case 3:
case 4:
result.push(start+i);
break;
}
}
console.log(result);

Return missing number in array of integers 1 - 9 [closed]

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 12 months ago.
Improve this question
I ran the code using different arrays missing a number between 1 - 9, but it kept returning -1.
function findMissing(arrOfNumbers) {
const integers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let index = 0; index < arrOfNumbers.length; index++) {
if (integers[index] !== arrOfNumbers[index]) {
return integers[index];
}
return -1;
}
}
console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9])); //returns -1 instead of 5
If I understood the problem, you want missing elements from array 2. if so, simply use,
arr1.filter(x=> !arr2.includes(x));
In your case
const array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const array2 = [0, 1, 3, 4, 6, 7, 8, 9];
const result = array1.filter(x=> !array2.includes(x)); // returns 2 , 5
console.log(result)
You can spread the array over an object and compare the key and value pairs of the object. If key and value are different then that means some value is missing in the input array that is out of range. Note the use of !=, it is intentional to compare values and not types.
function findMissing(arrOfNumbers) {
const obj = {...arrOfNumbers};
for(const [key, val] of Object.entries(obj)) {
if(key != val) {
return key;
}
}
return -1;
}
// returns 2 since it is the first missing value in the order
console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9]));

Javascript Select vector with the greatest number of items [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 years ago.
Improve this question
I have 4 arrays:
Note: Length of array length is different in any session
var Class_A = [8, 11, 3, 9, 11, 16, 16];
var Class_B = [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77]; // Greatest length with 11 items,
var Class_C = [10, 14, 19];
var Class_D = [8, 2];
I need to select array with greatest number of items, (Class_B)
My expectation is:
var major = number major of array.length (in this case Class_B);
My code:
var major = max(Class_A.length > Class_B.length > Class_C.length > Class_D.length);
Edit
An unorthodox solution, from a newbie but it works.
var Class_A = [8, 11, 3, 9, 11, 16, 54, 14, 5];
var Class_B = [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77, 16, 7]; // Greatest length with 11 items,
var Class_C = [10, 14, 19];
var Class_D = [8, 2];
var Classes = [];
Classes.push(Class_A.length) ;
Classes.push(Class_B.length) ;
Classes.push(Class_C.length) ;
Classes.push(Class_D.length) ;
console.log("Lenght of 4 arrays>"+Classes);
Classes.sort(function(a, b) { // order by major number
return a - b;
});
Classes.reverse(); // reverse array
console.log("Total>"+Classes); // print sort by major number
console.log("Major>"+Classes[0]); // Works => the magic number that I need
Put all your arrays in an array.
classes = [Class_A, Class_B, Class_C, Class_D];
Then, to get the largest one:
classes.reduce((acc, curr) => curr.length > acc.length ? curr : acc);
// => [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77]
EDIT:
Or, if you want length of the largest array:
classes.reduce((acc, curr) => curr.length > acc ? curr.length : acc, 0);
// => 11

Splitting arrays into chunks and repeating the previous elements [closed]

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 2 years ago.
Improve this question
I am trying to wrap my head around the following:
We have the following example array: var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
We have user input N as a parameter.
For the sake of this example, let's assume that N = 3.
After that, let's define the var blockSize = 2 * N
The example array arr then has to be split into chunks like these:
[0, 0, 0, 1, 2, 3], [1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11, 12], [10, 11, 12, 0, 0 ,0]
Note that this is a perfect example where numbers get split perfectly because of the parameter we gave. I need this to be working even with cases, where it won't be so perfect. In that case, zeros should be added to the end of the last chunk to have it properly sized (N*2).
You can slice in a loop:
function overlappingChunks(arr, len) {
const chunks = [];
for (let i = 0; i < arr.length; i += len) {
const chunk = arr.slice(i, i + len * 2);
chunks.push(chunk.concat(new Array(len * 2 - chunk.length).fill(0)));
}
return chunks;
}

JavaScript - Get only negative numbers form array [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have task: Write the function getNegativeNumbers (array) which as an argument take array. The function has to return a new array filled only negative numbers.
I have done this solution, but I ask about different methods.
var myArray = [4, -5, 0, 2, -67, 8, 10, -34 ];
function getNegativeNumbers(array) {
var negatives = [];
for (var i = 0; i < array.length; i++) {
if (array[i] < 0) {
negatives.push(array[i]);
}
}
return negatives;
}
console.log(getNegativeNumbers(myArray));
Try following
var myArray = [4, -5, 0, 2, -67, 8, 10, -34];
function getNegativeNumbers(array) {
return array.filter(function(value) {
return value < 0;
});
}
console.log(getNegativeNumbers(myArray));
ES6
var myArray = [4, -5, 0, 2, -67, 8, 10, -34];
function getNegativeNumbers(array) {
return array.filter(value => value < 0);
}
console.log(getNegativeNumbers(myArray));
For reference - filter
Use Array#filter to create a new array of negative numbers:
var myArray = [4, -5, 0, 2, -67, 8, 10, -34 ];
var result = myArray.filter(function(number) {
return number < 0;
});
console.log(result);
Use Array.filter
var myArray = [4, -5, 0, 2, -67, 8, 10, -34 ];
var negatives = myArray.filter(e => e < 0);
console.log(negatives);

Categories