javascriptnode.jsreadline-sync

How do I get my JavaScript cash machine simulator to work in my Node.js terminal?


I am currently making a JavaScript Node.js cash machine simulator using the 'readline-sync' library to handle terminal input/output. The application allows the user to check their balance, deposit or withdraw money. I have used a loop to keep the application running until the user decides to exit. I've also included error handling for invalid inputs e.g. withdrawing more than the total balance. However, when I type 'node cashmachinesimulator.js' into my VS Code terminal, it immediately exists with no output. Can anyone help make my code better, please?

Here's my code so far:

const cashMachine = require('readline-sync');

let name,
  correct_pass = (/^[0-9]{4}£/),
  passTry = 3,
  currentBalance = 20000;

// Input a username
function cashMachineName() {
  name = cashMachine.question("Enter your name");
  if (name !== "" && name !== null) {
    cashMachinePassword();
  } else {
    cashMachineName();
  }
}

// Input a valid password
function cashMachinePassword() {
  let passwordEntry = questionInt("Hello " + name + ", please enter your 4-digit PIN");
  checkPassword(passwordEntry);
}

// Verify password meets requirements
function checkPassword(userInput) {
  if (correct_pass.test(userInput)) {
    selectAccountType();
  } else {
    while (!(correct_pass.test(userInput))) {
      if (passTry === 1) {
        console.log("Incorrect PIN");
        console.log("Maximum tries exceeded! Your account has been locked. Contact your bank for support.");
        exit();
        break;
      } else {
        passTry -= 1;
        alert("Incorrect PIN. Please try again.");
        alert("You have " + passTry + " chance/s to try");
        cashMachinePassword();
      }
    }
  }
}

// Select which account to use
function selectAccountType() {
  let accountType = questionInt("Which type of account do you have? \n 1. Savings \n 2. Current \n 3. Credit");
  if (accountType !== "" && accountType !== null && !isNan(accountType)) {
    switch (accountType) {
      case 1:
        balance();
        break;

      case 2:
        withdrawal();
        break;

      case 3:
        deposit();
        break;

      case 4:
        exit();
        break;

      default:
        cashMachine.questionInt("Please make a valid operation");
        selectFunction();
    }
  } else {
    cashMachine.questionInt("Please make a valid selection");
    selectFunction();
  }
}

// Balance 
function balance() {
  cashMachine.questionInt("Your current balance is £" + currentBalance);
  toContinue();
}

// Deposit
function deposit() {
  let depositAmount = questionInt("How much do you want to deposit?");
  if (depositAmount !== "" && depositAmount !== null && !isNaN(depositAmount)) {
    currentBalance += depositAmount;
    console.log("You have successfully deposited £" + depositAmount + "\n" + "You know have £" + currentBalance);
    toContinue();
  } else {
    console.log("Error: Please enter a number!");
    deposit();
  }
}

// Withdrawal
function withdrawal() {
  let withdrawalAmount = questionInt("How much do you want to withdraw? \n" + "The minimum amount you can withdraw is £5");
  if (withdrawalAmount !== "" && withdrawalAmount !== null && !isNaN(withdrawalAmount)) {
    if (withdrawalAmount >= 5) {
      if (withdrawalAmount <= currentBalance) {
        currentBalance -= withdrawalAmount;
        console.log("Transaction successful!");
        console.log("Your remaining balance is £" + currentBalance);
        toContinue();
      } else {
        console.log("You do not have sufficent funds!");
        withdrawal();
      }
    } else {
      console.log("You must withdraw at least £5");
      withdrawal();
    }
  } else {
    console.log("Error: Please enter a number!");
    withdrawal();
  }
}

// Check if the user wants to perform another transaction
function toContinue() {
  let yesOrNo = questionInt("Do you want to perform another transaction? \n 1. Yes \n 2. No");
  if (yesOrNo !== "" && yesOrNo !== null) {
    if (yesOrNo === 2) {
      exit();
    } else {
      selectAccountType();
    }
  } else {
    console.log("Please make a valid selection");
    toContinue();
  }
}

// Exit the cash machine simulator
function exit() {
  console.log("Thank you for using our cash machine");
  // To simulate a real cash machine, get ready for next user
  // cashMachineName();
}```

Solution

  • hello

    you made some Critical mistakes .

    1. you didn't call the main function in your case it's cashMachineName() .
    2. you imported(require) readline-sync module as cashMachine object and you used it's members the questionInt() and others methodes without link it with the it's object cashMachine , the correct way : cashMachine.questionInt() .
    3. you used the alert() function and this wrong in nodejs this web api function there isn't alert() function in nodejs .

    alert() in member of window object in web in nodejs we have global object instead . learn more from MDN

    1. you have some errors also in the code syntax and logic.

    really i think you make this mistakes by mistake :) , maybe you fixed it already but i wanted to answer you , i hope this was useful for you .