Make a fully functioning dictionary using JavaScript from scratch!!
Hey guys and welcome back to The Decoderzz!!
Today we're gonna make a fully functioning dictionary using JavaScript from scratch!!
So further ado, let's get started!!
REQUIREMENTS:-
1. Node.js should be installed in your computer. If you don't have node.js then install it from Here.
2. Node.js module 'Prompt-sync' should be installed in your computer. It doesn't come pre-installed with Node.js so if you have it, you probably know that you have it.
To download Node.js Prompt-sync module, follow the command given below:
npm install prompt-sync
Now if you have followed the above requirement steps properly, you are good to go.
Step 1:- Make a file with the name 'Dictionary.js'
Step 2:- Open notepad or any other code editor.
Step 3:- To import the prompt-sync module, type in this line given below:
const prompt = require('prompt-sync')();
Step 4:- Make a JavaScript object (We will call this object as Dictionary from this point forth) in the syntax given below:
let dict = {
key1: "value1",
key2: "value2", key3: "value3",
keyn: "valuen"
};
The word that the user will enter to search goes to the 'key' and the result that the dictionary will show goes to the 'value'.
If key is more than one word then it should be written inside quotes.
Value is always written inside quotes.
The dictionary keys and values that we'll use in this tutorial are as follows:
let dict = {
halo: "A ring of light formed around any bright celestial object like the Sun or
Moon because of the Earth's atmosphere",
aurora: "The Northern or Southern Lights formed due to solar and cosmic radiations
getting trapped into a planet's magnetic field which on collision with the
atmosphere ionises the particles in there which then emit light",
nodejs: "It is a runtime for JavaScript outside of a browser that lets us make
these simple and awesome projects"
};
Step 5:- Now make a result function like this:
function result() {
}
Step 6:- Take the search word as an input by the user inside a variable using the prompt-sync module:
let word = prompt("Enter your word to search: ").toLowerCase();
Note that we have lowercased the user-input so that it doesn't throw an error as the dictionary keys are case sensitive.
Step 7:- To display the result we take the value of the searched word from the dictionary (if it exists).
The syntax of taking a value out of an object is as follows:
dict[key];
here 'dict' is the name of the variable that contains the object and 'key' is the key of the object.
In our case, we check if the user-input word matches the key of our dictionary:
let result = dict[word];
if (result!=undefined) {
console.log(result);
}
else {
console.log("Sorry, the meaning of this word doesn't exist in our dictionary");
}
Step 8:- So the content inside our result function is as follows:
function result() {
let word = prompt("Enter your word to search: ").toLowerCase();
let result = dict[word];
if (result!=undefined) {
console.log(result);
}
else {
console.log("Sorry, the meaning of this word doesn't exist in our dictionary");
}
}
result();
To execute the result function, we add the following code after closing the result function:
result();
Step 8:- After this, our dictionary is pretty much functional but we don't wanna stop here because we can add another feature to this dictionary.
As we use this dictionary now, it will show us the result if the searched word exists in the dictionary but after showing the result, the dictionary immediately closes and we have to manually start it again to search another word.
So to tackle this, we are gonna add a confirmation option so that if the user wants to search more words then they can and if not then they can close the dictionary by themselves.
So to do that we make another function with the name confirm and ask the user if they want to continue searching or to close the dictionary in a command-line manner.
The code is as follows:
function confirm() {
let confirmation = prompt("Do you want to search again? (Y/N)");
let yes_no = confirmation.toLowerCase();
if (yes_no=="y" || yes_no=="n") {
if (yes_no=="y") {
result();
confirm();
}
}
else{
console.log("Please type Y or N only");
}
Here, we take the user input in the 'confirmation' variable and make it lowercase to avoid any errors and then take the final value in the 'yes_no' variable.
Now we check if the user-input is Y/N or something else as the user can input any string.
If the user inputs Y as their answer, we execute the result function followed by the confirm once again.
If the user inputs N, it simply closes the dictionary.
We now add this confirm function before the result function execution code so that while again running the result function, it doesn't try to run twice and throw us an error.
We can also add a timeout for the confirmation to appear so that the user has enough time to read the search result but this is optional.
So, here we have it. We have a fully functional dictionary ready to tackle Oxford (just joking :D)...
Here is the full code all in its glory:-
const prompt = require('prompt-sync')();
let dict = {
halo: "A ring of light formed around any bright celestial object like the Sun or
Moon because of the Earth's atmosphere",
aurora: "The Northern or Southern Lights formed due to solar and cosmic radiations
getting trapped into a planet's magnetic field which on collision with the
atmosphere ionises the particles in there which then emit light",
nodejs: "It is a runtime for JavaScript outside of a browser that lets us make
these simple and awesome projects"
};
function result() {
let word = prompt("Enter your word to search: ").toLowerCase();
let result = dict[word];
if (result!=undefined) {
console.log(result);
}
else {
console.log("Sorry, the meaning of this word doesn't exist in our dictionary");
}
}
function confirm() {
let confirmation = prompt("Do you want to search again? (Y/N)");
let yes_no = confirmation.toLowerCase();
if (yes_no=="y" || yes_no=="n") {
if (yes_no=="y") {
result();
confirm();
}
}
else{
console.log("Please type Y or N only");
}
}
setTimeout(confirm, 3000);
result();
Here are some screenshots of the dictionary in action!!
![]() |
Here we have written each letter of the word NodeJS randomly in uppercase and lowercase to show that it doesn't throw error. |
![]() |
By pressing 'N', the dictionary closes. |
So, now we have a fully functional dictionary.
Hope y'all love it.
Thank you and have a nice day :D
op!!!!!
ReplyDeleteOppppppp
ReplyDelete