I'm trying to return an array of numbers
function numbers(l, r) {
// l and r are any given numbers
var x=[];
var i=l;
while(x.push(i++)<r){};
return x;
}
console.log(numbers(10, 19));
I'm trying to return an array of numbers
function numbers(l, r) {
// l and r are any given numbers
var x=[];
var i=l;
while(x.push(i++)<r){};
return x;
}
console.log(numbers(10, 19));
So far so good. Now I want to get the odd numbers. How can I achieve that?
Share Improve this question edited Aug 8, 2017 at 14:29 georg 215k56 gold badges322 silver badges398 bronze badges asked Aug 8, 2017 at 14:23 ST80ST80 3,90317 gold badges71 silver badges143 bronze badges 3 |7 Answers
Reset to default 10x.filter(n => n%2)
will keep only odd numbers.
if n
is even, n%2
will return 0 and the item will be removed by the filter.
let arr = [1,2,3,4,5,6,7,8,9,10,11,12]
let odds = arr.filter(n => n%2)
console.log(odds)
function* numbers(start, end) {
let i = start%2 ? start : ++start;
while(i <= end) {
yield i;
i += 2
}
}
console.log([...numbers(2, 10)])
or
class Odd {
constructor(l, r) {
this.l = l;
this.r = r;
}
*[Symbol.iterator]() {
let i = this.l % 2 ? this.l : ++(this.l);
while (i <= this.r) {
yield i;
i += 2
}
}
}
const odd = new Odd(2,10);
console.log([...odd])
Provided two values l
(starting point) and r
(ending point) you would create your array from l
to r
in increments of +1. Use that array to filter the desired values that meet the mod 2
or % 2
criteria. FYI mod 2
returns 0
if the value is an even number or 1
if the value is an odd number. The filter() method creates a new array with all elements that pass the test implemented by the provided function (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). Do note that in JavaScript 0
is a falsy
value so only positive integer values like 1 are returned thus the array is formed with all values that resulted in n % 2
equal to 1.
function oddNumbers(l, r) {
let arr = [];
while (l <= r) {
arr.push(l);
l += 1;
};
return arr.filter(n => n % 2);
}
You could use an appropriate start value and increment by 2
for each pushing.
function numbers(l, r) {
var x = [],
i = Math.floor(l / 2) * 2 + 1; // start with an odd number
while(i <= r) {
x.push(i);
i += 2;
};
return x;
}
console.log(numbers(10, 19));
console.log(numbers(3, 5));
.as-console-wrapper { max-height: 100% !important; top: 0; }
At first, make i odd:
i = i+1-i%2;
Then iterate over every second:
while(x.push(i+=2)<r){};
Note that this returns r-1 numbers and not numbers up to r-1
Shorter
var EverySecond = (start,length) => Array.from({length}).map((el,i)=>start+i*2);
var even = (start,length) => EverySecond(start+start%2,length);
var odd = (start,length) => EverySecond(start+1-start%2,length);
1 - first create an array with all numbers between n and p.
2- create a second array to collect all odd numbers using For loop.
In For loop, collect all odd numbers using Math.floor()
method. Any odd number divided by 2 will not be equal to the nearest integer created by Math.floor()
and thus, that number will be included in the second array. All the even numbers will be filtered out as that even number divided 2 and Math.floor()
integer divided by 2 will be of same value.
Code:
function numbers(n, p) {
let allArr = [];
for (let i = n; i <= p; i++) {
allArr.push(i);
}
let result = [];
for (let j = 0; j < allArr.length; j++) {
if (allArr[j] / 2 !== Math.floor(allArr[j] / 2)) {
result.push(allArr[j]);
}
}
return result;
}
console.log(numbers(10, 19));
Try this for pairs:
[0,1,2,3,4].filter(function(n){ return n%2 === 0; });
Try this for odds:
[0,1,2,3,4].filter(function(n){ return n%2 !== 0; });
2
toi
..? – Teemu Commented Aug 8, 2017 at 14:252n + 1
wheren
is a member of the integers – Patrick Barr Commented Aug 8, 2017 at 14:26