tags: JavaScript Maria DB Python

Pokemon DB Part 1
Pokemon DB Part 2
Pokemon DB Part 3
Pokemon DB Part 4
Pokemon DB Part 5

Getting the variables

In this chapter we will get the CardNo as well as the SetCount - values which most of the Pokemon Cards have.
There are exceptions to the rule and they are a pain to include and in some cases they are not available at all in the used TCGdex DB - but I could not find a more sophisticated source.

Alright back to coding


JavaScript

First interaction functions

I'll break it down again - sorry that the outputs are in German but I think you'll figure it out what each sentence means - if all else fails you can use a translation site of your liking.

//Input CardNo
async function askForCard() {
  while (true) {
    const input = await askQuestion('Bitte Kartennummer eingeben: ')
    CardNo = parseFloat(input)

    if (!isNaN(CardNo)) {
      console.log(`Es wurde ${CardNo} eingegeben`)
      rl.close
      return CardNo          
    } else {
      console.log("Keine Nummer bitte Nummer eingeben")
    }
  }
}
//Input SetCount
async function askForSet() {
  while (true) {
    const input = await askQuestion('Bitte Setnummer eingeben: ')
    SetCount = parseFloat(input)

    if (!isNaN(SetCount)) {
      console.log(`Es wurde ${SetCount} eingegeben`)
      rl.close
      return SetCount          
    } else {
      console.log("Keine Nummer bitte Nummer eingeben")
    }
  }
}

Lets see what we are doing here
We are creating two functions - one for asking which for the CardNo and one for the SetCount.
We are also checking if the input is a number - we cannot use any strings for what is to come.

Why async function? - We use this to be able to do other tasks while a the function is running - for further reference I would reccomend this mdn web docs Site

As you can see we are putting our initial setup and function to use - so we are calling a function within a function - how cool is that!

We are calling askQuestion with await askQuestion('Question') whereas the Question will be printed on the console
We are waiting for inputof the user / command line and that input becomes our variable CardNo but we are also adding a sort of failsafe to the whole thing!

This is the if Function in general this is done by a check - so basically if this then do that otherwise (else) do this. In this particular case we are checking if the received input is NOT NOT a number (yes I'll explain)
The check functions in the following way if (!isNaN(CardNo)) the !is a way to tell JS that we are checking if the function is NOT true and isNaNmeans is not a number. So in general if the input (CardNo) is a number we will execute the code afterward. If it is a number we will jump to the elsepart of the function.

The else part of the function just restarts the whole question and asks the user to put in a correct number.

In both functions we also need to close the ReadLine function in order to release the console for the next function to work.

Please keep in mind we are only creating the functions right now - we are NOT starting them - this comes at a later stage

Previous Post Next Post