In this repository, the code written in JS is for storing and printing NFTs (Non-Fungible Tokens) for a user. The code demonstrates how to create, manage, and display an NFT collection using JavaScript. Below is a step-by-step explanation of the code.
creating an empty array to store our NFTs.
let nfts = [];
A Set to keep track of the IDs that have already been used to ensure each NFT has a unique ID.
let usedIds = new Set();
This function generates a random ID between 1 and 1000 and checks if it has already been used. If it has, it generates a new one until it finds a unique ID.
function generateRandomId() {
let id;
do {
id = Math.floor(Math.random() * 1000) + 1; // Generate a random ID between 1 and 1000
} while (usedIds.has(id)); // Repeat if the ID is already used
usedIds.add(id); // Add the new unique ID to the Set
return id;
}
This function takes in parameters to create an NFT object with metadata (title, author, publicationYear, genre) and a unique ID. It then stores this NFT in the nfts
array.
function mintNFT(title, author, publicationYear, genre) {
let nft = {
id: generateRandomId(), // Assign a unique random ID
title: title,
author: author,
publicationYear: publicationYear,
genre: genre
};
// Adding NFT object to the array
nfts.push(nft);
}
This function lists all NFTs stored in the nfts
array in a tabular format.
function listNFTs() {
console.table(nfts);
}
This function returns the total number of NFTs created.
function getTotalSupply() {
return nfts.length;
}
Below there are some NFTs with Indian book details and then list them and print the total number of NFTs minted.
mintNFT("The God of Small Things", "Arundhati Roy", 1997, "Fiction");
mintNFT("Midnight's Children", "Salman Rushdie", 1981, "Historical Fiction");
mintNFT("The White Tiger", "Aravind Adiga", 2008, "Fiction");
listNFTs();
console.log(`Total NFTs minted: ${getTotalSupply()}`);
This project demonstrates how to manage a collection of NFTs, ensuring each one is unique and easily viewable. The code provides a basic framework for creating and displaying NFTs in a simple and organized manner.