157k views
2 votes
Write a program that reads an integer (greater than 0) from the console and flips the digits of the number, using the arithmetic operators // and %, while only printing out the even digits in the flipped order. Make sure that your program works for any number of digits and that the output does not have a newline character at the end.

User Milissa
by
3.2k points

1 Answer

2 votes

Answer:

const stdin = process.openStdin();

const stdout = process.stdout;

const reverse = input => { //checks if input is number

if (isNaN(parseInt(input))) {

stdout.write("Input is not a number");

}

if (parseInt(input) <= 0) {. // checks if no. is positive

stdout.write("Input is not a positive number");

}

let arr = []; // pushing no. in array

input.split("").map(number => {

if (number % 2 == 0) {

arr.push(number);

}

});

console.log(arr.reverse().join("")); // reversing the array

};

stdout.write("Enter a number: ");

let input = null;

stdin.addListener("data", text => {

reverse(text.toString().trim()); //reversing

stdin.pause();

});

Step-by-step explanation:

In this program, we are taking input from the user and checking if it is a number and if it is positive. We will only push even numbers in the array and then reversing the array and showing it on the console.

User Sevenever
by
3.2k points