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);
Related
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 an array which looks like this [1,0,3,0,5,0] what I want is that I want to insert the zero elements the elements of this array [2,4,6] so the complete array should look like this [1,2,3,4,5,6].
let a = [1,0,3,0,5,0]
let b = [2,4,6]
// expected output [1,2,3,4,5,6]
You can also use forEach in this case for a mutating solution:
let a = [1, 0, 3, 0, 5, 0, 7, 0];
let b = [2, 4, 6, 8]
a.forEach((i, j) => {
if (i === 0)
a[j] = b[~~(j / 2)] // integer division
})
console.log(a)
You could take a variable for the index for finding falsy values and insert the replacement value at this index.
let data = [1, 0, 3, 0, 5, 0],
replacements = [2, 4, 6],
i = 0;
for (const value of replacements) {
while (data[i]) i++;
data[i] = value;
}
console.log(data);
For getting a new array, you could map the data array with the replacements.
let data = [1, 0, 3, 0, 5, 0],
replacements = [2, 4, 6],
result = data.map(v => v || replacements.shift());
console.log(result);
Below approach with work:
x = [1,0,3,0,5,0]
y = [2,4,6]
j = 0;
for(i = 0; i < x.length; i ++) {
if(x[i] === 0 && j < y.length)
x[i] = y[j++];
}
console.log(x);
You can do something like this:
const a = [1,0,3,0,5,0];
const b = [2,4,6];
let lastIndex = 0;
for (let i = 0; i < b.length; i++) {
lastIndex = a.indexOf(0, lastIndex);
a[lastIndex] = b[i];
}
console.log(a);
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 4 years ago.
Improve this question
So im trying to create a loop to log the even numbers produced in my array, i have this but i cant seem to figure out where im stuck, sorry for the basic question still learning.
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < myArray.length; i++) {
if (myArray % 2 === 0)
console.log(myArray[i]);
}
You're forgetting the [i] in the if clause:
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 === 0)
console.log(myArray[i]);
}
You are not getting array value correctly. Use index to access array value as myArray[i]
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 === 0)
console.log(myArray[i]);
}
Try this. You forgot myArray[i] inside if.
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 === 0)
console.log(myArray[i]);
}
As many have already answered, you forgot the index of your array.
To improve your code, instead of a for loop you could filter your array:
const result = myArray.filter(i => i % 2 === 0)
console.log(result)
or if you wanted to log it on every iteration:
myArray.filter(i => {if(i % 2 === 0) console.log(i)})
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 6 years ago.
Improve this question
I saw an output of a loop that make the result: 0, 1, 10, 11, 12, 2, 3, 4, 5, etc.
I know the above loop is wrong and unusual and maybe you ask what's the usage of this loop, but i want to know how can i implement that.
Thanks
You could use numbers and sort it as strings with build in Array#sort without callback. This method uses sorting by string.
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
array.sort();
console.log(array);
Or you could build a tree and read it with depth first alogrithm with priority of number property.
var i,
tree = {};
for (i = 0; i < 13; i++) {
i.toString().split('').reduce(function (r, c, i, cc) {
r[c] = r[c] || {};
if (i + 1 === cc.length) {
r[c].number = true;
}
return r[c];
}, tree);
}
Object.keys(tree).forEach(function iter(object, value) {
object.number && console.log(value);
return function(k) {
k === 'number' || Object.keys(object[k]).forEach(iter(object[k], value + k));
};
}(tree, ''));
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
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 8 years ago.
Improve this question
I have an array of numbers and I need to find the maximum slice of the array which contains no more than two different numbers.
so if I have
[1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]
the output I'm looking is 10, because the array slice of (0, 9) is the largest slice of the array with no more than two different numbers.
How do I achieve that?
This example does the trick for you. However I kindly ask other higher programming gods to improve this or provide another solution. I think this code could be greatly optimized. Please comment if you find bugs, or examples to which this function returns a faulty solution.
function returnLargestConsecutiveArraySlice(array)
{
//set an empty array.
var a = [];
//walk the supplied array and check if number is not repeated
array.filter(function (element, index, array) {
if (element != array[index-1] && element != array[index+1])
{
a.push(index);
return element;
}
}
);
//the returned array contains all indexes to the numbers that are not repeated.
//walk the array and check if number the next number matches the current one.
//If so the index is consecutive.
var numbers = a;
var b = [[]]; //use an empty array to start with.
var index = 0;
for (var i = 0; i < numbers.length-1; i++){
if(numbers[i+1] == numbers[i]+1)
{
//number is consecutive, add.
b[index].push(numbers[i]);
}
else
{
//chain broken, start a new one.
index++;
b[index] = [];
}
}
//we now have an array with all chains. Look for the largest chain.
var largest = [];
for (var i = 0; i < b.length; i++)
{
if (b[i].length > largest.length)
{
largest = b[i];
}
}
//largest chain found. Slice the original array on the largest chain.
return array.slice(largest[0], largest[0] + largest.length+1);
}
console.log(returnLargestConsecutiveArraySlice([1, 1, 1, 2, 2, 2, 4, 5, 6, 1, 1, 7, 8, 9, 10, 11, 2, 2, 6, 2, 1, 8]));