Even digits within a number - simple function

Hello everyone. I’ve recently started learning JavaScript. Currently trying to learn it by solving simple problems. First let me start with the task:
" Write a function that takes an interval as two four-digit numbers,
e.g. 2000 and 3001 and outputs a list of all the numbers in the given interval, consisting of
even digits, for example:
[‘2000’, ‘2002’, ‘2004’, ‘2006’, ‘2008’, ‘2020’, ‘2022’, ‘2024’, ‘2026’ … ] "
So, I started with creating a logical steps, what should I do step by step.

  1. First I need to get input from the user - 2 numbers. In a case of the task it will be between 2000 and 3001

  2. I need to create a function, which will loop every digit of the number from the user and check whether function has even digits or not. I should do this step for both numbers. In the end I’ll have two functions

  3. Last step I need to create a function, which will receive numbers from the user, then will be looped through above mentioned two functions and then will give me a result of only even digits within a given range. Received result from the looping process will be stored to an empty array. Also I’ll add if statement, which will check if there is some odd digits, function should ignore it, if even digits it should push it to an empty array.

So this is how I tried to create a logic. Below is my code. I wasn’t able to solve it. So, if anyone can give a hand to solve it, would be great. Thanks in advance for your time and help!

let a = +prompt('Enter any number');
let b = +prompt('Enter any number');

let even = function (a) {
  a = String(a);
  for (let i = 0; i < a.length; i++) {
    if (a % 2 === 0) {
      true;
    } else {
      false;
    }
  }
  return a;
};

let even1 = function (b) {
  b = String(b);
  for (let i = 0; i < b.length; i++) {
    if (b % 2 !== 0) {
      true;
    } else {
      false;
    }
  }
  return b;
};

function getNumRange(even, even1) {
  let arr = [];
  for (even; even < even1.length; even++) {
    if (
      even[i] == '1' ||
      even[i] == '3' ||
      even[i] == '5' ||
      even[i] == '7' ||
      even[i] == '9'
    ) {
      even = even.replace(even[i], '');
    } else {
      arr.push(even);
    }
  }
  for (even1; even1 <= even.length; even1++) {
    if (
      even[i] == '1' ||
      even[i] == '3' ||
      even[i] == '5' ||
      even[i] == '7' ||
      even[i] == '9'
    ) {
      even1 = even1.replace(even1[i], '');
    } else {
      arr.push(even1);
    }
  }
  return arr;
}

console.log(getNumRange(even(), even1()));

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.