0

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?

3
  • 2
    You're basically asking, how to add 2 to i ..? Commented Aug 8, 2017 at 14:25
  • note odd numbers can be described as 2n + 1 where n is a member of the integers Commented Aug 8, 2017 at 14:26
  • "So far so good." - sure? Try running your code. Commented Aug 8, 2017 at 14:29

7 Answers 7

10

x.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)

Sign up to request clarification or add additional context in comments.

1 Comment

OP's question accept 2 numbers not, an array!
2

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])

Comments

2

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);
}

Comments

1

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; }

Comments

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 Comment

Do we need to make sure l is odd? (To ensure i is odd)
0

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));

Comments

-1

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; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.