HackerRank JavaScript Basic Certification Solution

In this blog post, you will find the HackerRank JavaScript Basic certification Solutions. The answers to questions are:-

hackerrank javascript basic certification solution

HackerRank JavaScript Basic Certifiation Solution :-

fizz-buzz.js

‘use strict’

function fizzBuzz(number) {
    for (let i = 1 ; i <= number ; i++) {
          if (i % 3 === 0 && i % 5 === 0) {
                   console.log(‘FizzBuzz’);
          } else if (i % 3 === 0) {
                  console.log(‘Fizz’);
          } else if (i % 5 === 0) {
                   console.log(‘Buzz’);
          } else {
                    console.log(i);
                    }
             }
    }

  fizzBuzz(10);

notes-store.js

‘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();
}

order-list-processing.js

‘use strict’;

function processOrderList(orderList, orderId, state) {
      return state === ‘Processing’ ?
      orderList.map(item => ({
           …item,
           state: item.id === orderId ? ‘Processing’ : item.state
       })) :
     orderList.filter(item => item.id !== orderId);
}

user-warning-data

class User {
     constructor(username) {
              this.name = username;
     }

     getUsername () {
             return this.name;
     }

    setUsername(username) {
             this.name = username;
     }
}

class ChatUser extends User {
       constructor(userName) {
               super(userName;
               this.count = 0;
         }

         giveWarning() {
              let count = this.count + 1;
              this.count = count;
         }

        getWarningCount() {
              return this.count;

        }
}

step-counter.js

const counter = (
function counter() {

    let value = 0;
    return {
         getValue: function() {
             return value;
         },
        changeBy: function (k) {
              value += k;
         }
    }
}
)();

function getFixedCounter(k) {
    let myCounter = counter;
    return {
        increment: () => {
            myCounter.changeBy(k);
        },
       decrement: () => {
            myCounter.changeBy(-k);
       },
      getValue: () => {
          return myCounter.getValue();
     }
  }
}

console.log(counter.getValue());
counter.changeBy(10);
counter.changeBy(20);
console.log(counter.getValue());

Hope this blog post was helpful to you and you have got the answer to your question. Thank you for visiting our blog. If you have any doubts about any coding questions then let us know in the comments section we will answer them as soon as possible. Till then check out our more blog posts.

Leave a Comment

Your email address will not be published. Required fields are marked *