JavaScript
Maria DB
Python
Pokemon DB Part 1
Pokemon DB Part 2
Pokemon DB Part 3
Pokemon DB Part 4
Pokemon DB Part 5
In this chapter we will check for Duplicate Sets - this is required so we can find out which is the correct one - otherwise the program would use the first set which it can find.
Alright back to coding
This time I will break it down in small pieces and present the code when everything is explained
In this function we are utilizing our first two functions - we need our variables for what is coming next.
//Check for Duplicate Sets
async function checkDupes() {
CardNo = await askForCard()
SetCount = await askForSet()
In order to find out if we have duplicate sets (SetCounts) we need to check our count with all available sets there are...
//Check for Duplicate Official Card Counts
try {
const sets = await tcgdex.fetch('sets');
const duplicateSets = [];
// Check for Duplicate Official Card Counts of SetCount
for (const item of sets) {
const officialCount = item.cardCount.official;
if (officialCount === SetCount) {
duplicateSets.push({ SetID: item.id, Name: item.name });
}
}
We are achieving this with const sets = await tcgdex.fetch('sets');
We are fetching all available sets from the TCGdex Database - if you would write this into a JSON file it would look something like this
{
"id": "sv03",
"name": "Obsidian Flammen",
"logo": "https://assets.tcgdex.net/de/sv/sv03/logo",
"symbol": "https://assets.tcgdex.net/univ/sv/sv03/symbol",
"cardCount": {
"total": 230,
"official": 197
}
},
{
"id": "sv03.5",
"name": "151",
"logo": "https://assets.tcgdex.net/de/sv/sv03.5/logo",
"symbol": "https://assets.tcgdex.net/univ/sv/sv03.5/symbol",
"cardCount": {
"total": 207,
"official": 165
}
},
Just image a whole lot more - in fact we will write this JSON a little bit further down the road
We are then creating an array with const duplicateSets = [];
where we'll write the data we'll need.
We wrote the whole JSON in the sets
variable - now we will shift through it and compare our variable SetCount
with the item official
If the numbers are the same - we are checking this with ===
we will push the data we need in our duplicateSets
array. In this case item.id
and item.name
(in the JSON above this would be sv03
and 197
if our SetCount
would be 197)
for (const item of sets) {
const officialCount = item.cardCount.official;
if (officialCount === SetCount) {
duplicateSets.push({ SetID: item.id, Name: item.name });
}
If we are lucky we will only write one Set into our array so we need to check how many Sets there are in our array
So if our array duplicateSets
is larger than 1 we will ask our user which set we are talking about
if (duplicateSets.length > 1) {
console.log('Hier sind die mehrfachen Sets:');
duplicateSets.forEach((set, index) => {
console.log(`${index + 1}. SetID: ${set.SetID}, Name: ${set.Name}`);
});
We achieve this in the following way duplicateSets.forEach((set, index)
this uses the set as well as the index - remember that computers start counting with 0 - well that is why we are adding a one to the whole index in the output
We gave the user a list of Sets with an index 1 - n and we will ask for the choice for this we will define the variable with let choice
and check again if it is a number with isNaN
together with checking if the given number is in the correct range.
We acquire this with if (isNaN(choice) || choice < 1 || choice > duplicateSets.length)
try {
let choice;
do {
// Wait for the user's input
choice = await question(`Welches Set willst du verwenden (1-${duplicateSets.length}): `);
choice = parseInt(choice);
// Check if userInput is valid
if (isNaN(choice) || choice < 1 || choice > duplicateSets.length) {
console.log(`Falsche Eingabe bitte 1 - ${duplicateSets.length} eingeben`);
}
} while (isNaN(choice) || choice < 1 || choice > duplicateSets.length);
// Return the chosen SetID
return duplicateSets[choice - 1].SetID;
} finally {
// just do nothing
}
} else {
//Return the only SetId
console.log('Es gibt keine mehrfachen Sets für SetCount', SetCount);
return duplicateSets[0].SetID;
}
} catch (error) {
console.error('Error fetching sets:', error);
return null;
}
}
If the choice is correct we will return duplicateSets[choice -1].SetID
(still the thing with starting to count from zero)
If we only have one Set we will just return duplicateSets[0].SetID;
Keep in mind we will return the id
part from the JSON file - e.g. sv03
or sv03.5
In this section we used a new way to define a variable - in the first section we only used const
which translates to constant - these values will be defined once and will / should never change - with let
the whole thing is different you can change this in every set of the code.
We used try in this section a whole lot - with this we can define what is happening when something goes wrong in the code the try statement just tells the computer to try something and if this goes wrong tell me (in my defined words) what was wrong
In the last step I only used try and finally this is because we are always getting our correct answer since we wont allow and wrong one - but the try statement requires either a catch or a finally statement at the end.
//Check for Duplicate Sets
async function checkDupes() {
CardNo = await askForCard()
SetCount = await askForSet()
//Check for Duplicate Official Card Counts
try {
const sets = await tcgdex.fetch('sets');
const duplicateSets = [];
// Check for Duplicate Official Card Counts of SetCount
for (const item of sets) {
const officialCount = item.cardCount.official;
if (officialCount === SetCount) {
duplicateSets.push({ SetID: item.id, Name: item.name });
}
}
if (duplicateSets.length > 1) {
console.log('Hier sind die mehrfachen Sets:');
duplicateSets.forEach((set, index) => {
console.log(`${index + 1}. SetID: ${set.SetID}, Name: ${set.Name}`);
});
try {
let choice;
do {
// Wait for the user's input
choice = await question(`Welches Set willst du verwenden (1-${duplicateSets.length}): `);
choice = parseInt(choice);
// Check if userInput is valid
if (isNaN(choice) || choice < 1 || choice > duplicateSets.length) {
console.log(`Invalid input. Please enter a number between 1 and ${duplicateSets.length}.`);
}
} while (isNaN(choice) || choice < 1 || choice > duplicateSets.length);
// Return the chosen SetID
return duplicateSets[choice - 1].SetID;
} finally {
// Enyjoy life
}
} else {
//Return the only SetId
console.log('Es gibt keine mehrfachen Sets für SetCount', SetCount);
return duplicateSets[0].SetID;
}
} catch (error) {
console.error('Error fetching sets:', error);
return null;
}
}
More things to come in part 4