MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

NodeJS Program to Process Exit Codes with Explanation

NodeJS Easy Node Basics & Modules 35 views
This problem helps you practice core NodeJS fundamentals in a practical way. It builds intuition around process, exit, code. Let’s break it down step by step so you can implement it confidently.
Back to Questions

Problem Statement

Exit with code 1 when an input number is negative.

Input Format

stdin: one integer.

Output Format

Print OK or ERROR.

Constraints

Use process.exitCode instead of process.exit.

Code Solution

This explanation is written for learning purposes and to help beginners understand the concept clearly.
const fs = require('fs'); const s = fs.readFileSync(0, 'utf8').trim(); if (!s) process.exit(0); const n = Number(s); if (n < 0) { console.log('ERROR'); process.exitCode = 1; } else { console.log('OK'); }

Output Example

Input:
-3
Output:
ERROR

Common Mistakes

- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).

Notes & Extra Practice

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next