Problem 22. specialrange.js ( ****Please Revise******)
/*Question
Special Range
Given a range, as [m, n] both inclusive, print all non-negative integers in the range.
Input
First line contains an integer which is starting value of range, say m
Second line contains an integer which is ending value of range, say n
Output
Print all non-negative integers in that range. One integer per line.
If no such integers exist, print -1.
Example
Input:
-5
4
Output:
0
1
2
3
4
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
//..............................................................................................................................
let startingValue = parseInt(readLine()); //-5
let endingValue = parseInt(readLine()); // 4
let mark = false;// we assume that there are no non-negative values present
let i = startingValue;// intilization of while loop
while (i <= endingValue) {
if (i >= 0) //-5>=0 false, -4>=0 false, -3>=0 false, -2>=0 false, -1>=0 false, 0>=0 true, 1>=0 true, 2>=0 true and so on
{
console.log(i)
mark = true; // since we have found all non-negative integers inside the range we
change the value of mark to true
}
i++; // increment of the while loop
}
if (mark == false) {
console.log(-1);
}
==============================================================================================
TERMINAL:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node specialrange.js <input.txt
0
1
2
3
4
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment