Notes Store Using JavaScript | Hacker Rank JavaScript (Basic) Certification Question

Hello beginner JavaScript Developer,
In this article, we will see how you can create a notes store using javascript. This question is also asked in the Hacker Rank JavaScript (Basic) certification. Programming is a fantastic journey from learning to working, having a certification doesn't only boost your value but also gives you the enthusiasm to learn more about new programming concepts. 

 

 

At the end of this article I will give you some bonus tips that will help you out to crack the hacker rank certification.

Note:- 

This article doesn't promote copying code and getting the certification on hacker rank or any other platform. The article is about giving you an overview of questions and steps to solve the problem statement.

 

And without further ado, let's drive into the problem statement:- 

 

Problem:- 

The task is to create a notes store class that will manage the collection of notes, with each note having a state (completed, active, others) and a name.

You must have to implement the following methods in the class:-

 

1. addNote(state, name):- This function will add a note in the collection with the following validations:- 

  • If the name is empty, then the function will throw an Error message as "Name cannot be empty"
  • If the name is non-empty but the state is not valid (complete, active, others) for a note, then the function will throw an Error message as "Invalid State {state} "

2. getNotes(state):-  This function will return an array of names of notes with the given state. The names are returned in the order the corresponding notes were added. In addition to the following validations.

  • If the state is not a valid note state, then the function will throw an Error message as "Invalid state {state}". 
  • If no notes are found in the given state, it returns an empty array.

 

Solution Approach:-

Note:- Read the Problem statement carefully and divide it into sub-components.
 

As mentioned in the problem statement we have to create a NoteStore class that will manage the collection of notes. It means we need a collection type of variable that will store the information of the notes.
In javascript, we have mainly two types of collection (Map and Set). Now that's on you what you love to those. My recommendation will be to go with Map (mostly used).


Now let's have look at the code for this component.


class NoteStore{
    constructor(){
        // collection for storing the notes
        this.notesCollection = Map()
        
        // array of valid state name
        this.validState = ['completed', 'active', 'others']
	}
}

In the above code, we have also created a validState array that will help us out invalidating the state value in the addNote and getNotes functions.


Now our second component of the problem statement is the addNote function where we will validate the data (state, name) and add valid blog name and state value in the notesCollection using the set function of the Map collection.


Let's see how the code will look like for this component.

addNote(state, name){
        if(name === "") throw "Error: Name cannot be empty";
        var state_flag = false;
        // if state flag become true after iterating over validState array, it means note has a valid state.
        this.validState.forEach(value => {
        	if (value === state){
        		state_flag = true;
        	}
        })
        // if state_flag doesn't become true, it means it's not valid state.
        if( state_flag !== true) throw `Error: Invalid state ${state}`;
        // adding note and state value in notesCollection.
        this.notesCollection.set(this.notesCollection.size, {
        	'name': name,
        	'state': state
        })
	}

 
That's cool we successfully implemented the addNote function which looks fine and satisfies all required conditions given in the problem statement.

The last component of the problem statement is creating a getNotes function that will return an array of all notes that are set with the given state else it will return an empty array.

let's see how code will look like for this component:-
 
getNotes(state){
        // array variable for storing the name of notes
        var resultNotes = [];
        // state_flag for performing the validation of state
        var state_flag = false;

        this.validState.forEach(value => {
        	if (value === state){
        		state_flag = true;
        	}
        })
        if(state_flag !== true) throw `Error: Invalid state ${state}`;
        
        // if any note in the notesCollection having the state value set as given state
        // add the note value in the resultNotes arr
        this.notesCollection.forEach(value => {
        	if(value.state === state){
        		resultNotes.push(value.name);
        	}
        })
        return resultNotes;
	}

 

Well done you have successfully solved the problem in no time. But a Special bonus tip for you is still here.


Bonus Tip:- 

  • You may don't know that on Hacker Rank you don't need to solve both problems to get the certification. If you got one question, you are good to go.
  • Whenever going for certification always take look at both problems and choose the one that is under your leg. If you have good knowledge of the subject then attempting both questions isn't bad.

 

So I hope this problem statement has given you an overall overview of the JavaScript(Basic) Certification Question on Hacker Rank and you have also learned how you can build a notes store using JavaScript.

 

If you want to have more blogs related to javascript, reactjs, and web development, you can have look at the below links.

 

Thank you for reading the blog.

Happy Coding...

Post a Comment

3 Comments

If you have any doubt, Let me Know

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)