From a6d50f9a2a5dc022fb013bae003e7dbbe9413b1c Mon Sep 17 00:00:00 2001 From: Hyder Date: Mon, 25 Aug 2025 19:43:05 +0300 Subject: Im trying to learn JS, so THIS --- NGG/inputfunc.js | 17 +++++++++++++++++ NGG/main.js | 38 ++++++++++++++++++++++++++++++++++++++ README | 1 + 3 files changed, 56 insertions(+) create mode 100644 NGG/inputfunc.js create mode 100644 NGG/main.js diff --git a/NGG/inputfunc.js b/NGG/inputfunc.js new file mode 100644 index 0000000..c917ec5 --- /dev/null +++ b/NGG/inputfunc.js @@ -0,0 +1,17 @@ +const readline = require('readline'); + +function input(prompt) { + const read_instance = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + return new Promise((resolve) => { + read_instance.question(prompt, (val) => { + read_instance.close(); + resolve(val.trim()); + }); + }); +} + +module.exports = { input }; \ No newline at end of file diff --git a/NGG/main.js b/NGG/main.js new file mode 100644 index 0000000..555812a --- /dev/null +++ b/NGG/main.js @@ -0,0 +1,38 @@ +const inputFunc = require('./inputfunc'); + +function numberGuessingGame(solution, range) { + if (solution == range) { + console.log("Correct guess!, Hurray 😌"); + return 0; + } + else if (solution < range) { + console.log("Low guess 😁"); + } + else if (solution > range) { + console.log("High guess 😅"); + } + else { + console.log("Wrong input â˜šī¸"); + } +} + +(async () => { + const range = Math.floor(Math.random() * (20 - 1 + 1) + 1); + let attempts = 0; + console.log("Guess the number? range is (1 to 20) (type 0 to Quit)\n"); + while (true) { + let solution = await inputFunc.input("") + let num = parseInt(solution, 10); + if (num === 0) { + console.log("🤗🤗🤗"); + break; + } + if (numberGuessingGame(solution, range) == 0) { + attempts++; + console.log("Attempts: " + attempts); + break; + } + attempts++; + console.log("Attempts: " + attempts); + } +})(); \ No newline at end of file diff --git a/README b/README index 137086a..f67320f 100644 --- a/README +++ b/README @@ -14,3 +14,4 @@ CONTENTS - Palindrome words checker exercise, in ROOT/Palindrome/Palindrome.c - Sorting numbers with (selection sort), in ROOT/SelectionSort/SelectionSort.c - Made a simple Rock, Paper, Scissor game, in ROOT/RPS/rps.c +- Made a simple Number Guessing Game (NGG) using node.js in terminal to learn JS basics, in ROOT/NGG/* -- cgit v1.2.3