Cash Machine (JavaScript only)

It’s been a gloomy day here in the North West of England. All trains to get me to the bootcamp got cancelled so working from home it was.

Today’s materials were sent to me via Slack and I worked on the first challenge, which was JS if/else statements practice.

I then moved on to spend the whole day working on my cash machine to work the way I want it to be, utilising alert popups such as :

prompt(), alert()

Check the CodePen below. If you run the pen, the code will run as soon as you click it and you will see a series of alert popups that will prompt you to set up PIN number and add money to the account, then a random-generated account number will be given to you.

Also note, every time we call the function (or run the code), a new random-generated account number will be given. I am wondering if this needs a database for the number to be stored and ID’d – anyways, I will list down things I hope I could fix.

Things I want to work on:

  1. Cancelling prompt() to exit function.
  2. If we input PIN value as 0000 in the field, it will only be passed as 0 only. I want this to show as 0000 or be invalid PIN
  3. When given the 5 options, I would like it to show as error if the input entered is not any of those 5 options. – Fixed this by adding default statement
    • //this code executes when none of the case statements match.
      default: 
      
      alert("Please choose the correct option of 1, 2, 3, 4 or 5");
      
      options();
      
      break;

Things I learned through this exercise:

  1. Functions within functions and calling it within the function.

    • const inputAccount = () => {
      
       if (checkAccount()) {
      
        attempts = 3;
      
        inputPin();
      
       } else {
      
         if (attempts > 0) {
      
          alert("Error: account not found. Please enter a valid account number. You have " + attempts + " attempts left.");
      
          attempts--;
      
          inputAccount();
      
         } else {
      
           alert ("Invalid account number.")
      
         }
      
       }
      
      }
  2. switch Statementscreenshot 2019-01-23 at 19.32.42

    • For switch statement to work, I need to have 4 things:
      • the test expression – in the case above: option
      • the case statements – do the condition checking. Each specific case statement must have a value so in the case above: 1,2,3,4,5
      • the break statements – to tell JS to stop executing the code below it. If for instance, a break statement was not in place – code will for each case following the case that matches will be executed.
      • the default statement – will execute when none of the other case match. This will help picking up bugs.
    • switch statement works like if/else conditional statements.  For instance
      • if (option === 1) {
         withdraw ();
        } else if (option === 2) { ...
        
        //instead of writing those, we utilise switch statement
        switch (option) /*option is the test expression*/ { 
          case 1 /*the value*/:
            withdraw(); /*code to be executed*/
            break; /*to exit code when the case has been matched*/
          case 2: ....
  3. parseInt()

      • Data type conversion
      • It is a conversion function
      • Converts a string to an integer
      • It parses (or goes through) the string until it finds a character. For instance
        • parseInt("ab12") // NaN
          parseInt("12ab") // 12
      • It will possibly be better if I had used Number() function. But it has its own pros and cons.

  4. Creating random numbers that will only generate 6 digits

    • const accountNo = Math.floor(100000 + Math.random() * 999999);

It was a challenging exercise for me and I learned a variety of code blocks, methods and different way of writing functions.

Thanks for reading!


Written with ♥ by brewingcode girl.

Leave a comment