This JavaScript file is a simple implementation of a notes management system. It allows the user to add notes to different states and retrieve them. The states available are 'completed', 'active', and 'others'. The system is designed to handle input from the standard input and write the output to a file.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution is implemented using a class called 'NotesStore'. This class has methods to add a note to a specific state, retrieve notes from a specific state, validate the state, and get the list of notes for a specific state. The 'main' function reads operations from the standard input and performs the corresponding operation on the 'NotesStore' object. If an operation fails, it catches the error and writes it to the output file.
use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
const completedNotes = [];
const activeNotes = [];
const otherNotes = [];
class NotesStore {
addNote(state, name) {
if (name === undefined || name === null || name === '') {
throw new Error('Name cannot be empty');
}
if (!this.isValid(state)) {
throw new Error('Invalid state ' + state);
}
this.getNotesList(state).push(name);
}
getNotes(state) {
if (!this.isValid(state)) {
throw new Error('Invalid state ' + state);
}
return this.getNotesList(state);
}
getNotesList(state) {
if (state === 'completed') {
return completedNotes;
} else if (state === 'active') {
return activeNotes;
}
return otherNotes;
}
isValid(state) {
return state === 'completed'
|| state === 'active'
|| state === 'others';
}
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const obj = new NotesStore();
const operationCount = parseInt(readLine().trim());
for(let i = 1; i <= operationCount; i++) {
const operationInfo = readLine().trim().split(' ');
try {
if (operationInfo[0] === 'addNote') {
obj.addNote(operationInfo[1], operationInfo[2] || '');
} else if (operationInfo[0] === 'getNotes') {
const res = obj.getNotes(operationInfo[1]);
if (res.length === 0) {
ws.write('No Notes\n');
} else {
ws.write(`${res.join(',')}\n`);
}
}
} catch (e) {
ws.write(`${e}\n`);
}
}
ws.end();
}
If you have a HackerRank Certification test or a coding interview coming up, you can use CodeRankGPT to your advantage. It will assist you during your test and help ensure you get the certification or the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.