Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples-solutions-readline-esm): add examples for ESM solutions with readline #43

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/solutions-readline-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "examples-solutions-readline-esm",
"version": "0.0.0",
"type": "module",
"scripts": {
"start:1000": "node src/1000"
}
}
50 changes: 50 additions & 0 deletions examples/solutions-readline-esm/src/1000.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @fileoverview Baekjoon 1000
* @see https://www.acmicpc.net/problem/1000
*
* This file runs well in the local environment.
* However, if you submit this file to Baekjoon, it will result in a 'Runtime Error (Syntax Error)' in Baekjoon's Node.js environment.
* This is because Baekjoon does not support ESM modules. so we need to use CJS modules instead.
*/

// --------------------------------------------------------------------------------
// Import
// --------------------------------------------------------------------------------

import { createInterface } from 'node:readline';
import { stdin as input, stdout as output } from 'node:process';
import { EOL } from 'node:os';
import { log } from 'node:console';

// --------------------------------------------------------------------------------
// Declaration
// --------------------------------------------------------------------------------

const rl = createInterface({ input, output });

let inputFile = '';

// --------------------------------------------------------------------------------
// Event Listening
// --------------------------------------------------------------------------------

rl.on('line', line => {
inputFile += `${line}${EOL}`;
}).on('close', () => {
// eslint-disable-next-line no-use-before-define
solution(inputFile);
});

// --------------------------------------------------------------------------------
// Solution
// --------------------------------------------------------------------------------

// eslint-disable-next-line no-shadow
function solution(inputFile) {
const [a, b] = inputFile
.trim()
.split(' ')
.map(val => Number(val));

log(a + b);
}
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading