0

I am trying to write a code(function) that takes in two parameters a and b, and returns all the even number between a and b if a is greater than b else it should return the odd numbers between a and b.

this is my code.

function number_ranges (a, b) {

    let numbers = [];

    if (a > b){
        for (let i = b; i < a; i++){
            if (i > b){
                numbers.push(i);
            }
        }
    }else{
        for (let i = a; i < b; i++){
            if (i > a){
                numbers.push(i);
            }
        }
    }

    const result = numbers.filter(function(num){
        return a > b ? num % 2 === 0: num % 2 === 1;
    });
    return result;
}

I would like to see a different approach because i cant seem to be able to pass all the test cases

7
  • does the order matter? Commented Sep 27, 2018 at 13:57
  • 1
    Why are you pushing all of the number in and than filtering? There is no need.... Commented Sep 27, 2018 at 13:59
  • Do you have an example of a test case where it's not working? Commented Sep 27, 2018 at 14:02
  • if(i > a) so why not just start it at i=a+1 and not have to do that check? Commented Sep 27, 2018 at 14:02
  • no the order doesnt matter but naturally it would be sorted since i would start iterating from the lowest.....correct me if i am wrong though i am a newbie Commented Sep 27, 2018 at 14:36

2 Answers 2

1

You could take a sningle loop and a check if the value is even or or in combination with a check for the order.

function getEvenOdd(a, b) {
    var inc = +(a < b) || -1,
        i,
        result = [];
     for (i = a; i !== b + inc; i += inc) {
         if (+(inc === 1) !== i % 2) {
             result.push(i);
         }
     }
     return result;
}

// even
console.log(getEvenOdd(3, 10).join(' ')); // [4, 6, 8, 10]
console.log(getEvenOdd(4, 10).join(' ')); // [4, 6, 8, 10]
console.log(getEvenOdd(3, 9).join(' '));  // [4, 6, 8]
console.log(getEvenOdd(4, 9).join(' '));  // [4, 6, 8]

// odd
console.log(getEvenOdd(10, 3).join(' ')); // [9, 7, 5, 3]
console.log(getEvenOdd(10, 4).join(' ')); // [9, 7, 5]
console.log(getEvenOdd(9, 3).join(' '));  // [9, 7, 5, 3]
console.log(getEvenOdd(9, 4).join(' '));  // [9, 7, 5]

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

Comments

1

I used this function f=(a,b)=>[...ArrayMath.max(a,b))].map((q,i)=>i).filter(z=>(a<b?!(z%2):z%2)&&z>=Math.min(a,b))

I first create a range from 1 to b as follow [...Array(Math.max(a,b))].map((q,i)=>i)

Then I filter with a<b?!(z%2):z%2. If a greater than b then a keep the even numbers, else the odds.

Also I keep only those number greater or equal to the first parameter using &&z>=Math.min(a,b)

f=(a,b)=>[...Array(Math.max(a,b))].map((q,i)=>i).filter(z=>(a<b?!(z%2):z%2)&&z>=Math.min(a,b))
console.log(f(10, 5))
console.log(f(5, 10))

3 Comments

Your function returns evens when a < b, in the opposite case it returns an empty array, please check the code.
@Joe25 Thanks for noticing, I got it inverted (not too good at english :c).
yeah it works pretty nice except that it returns the last number for the even number filter ....it's supposed to return only numbers in between ignoring the start and end point

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.