JavaScript Basic Assesment
JavaScript Basic Assesment
1. Account Transaction
2. Data Finder
3. Fizz Buzz
4. Joined Logger
5. Note Store
6. Staff List
7. Step Counter
8. Strip Property
9. GitHub Solutions
1. Account Transactions
Create a solutions to maintain a bank account’s balance.
Implement the Account class that manages an account’s balance. The class has the following constructor
and methods:
1. The constructor Account(balance) where parameter balance denotes the initial balance of the
account.
2. The method debit(amount) debits the amount from the account and returns true. If insufficient
balance, do not debit and returns false.
3. The method getBalance() returns the current balance.
4. The method credit(amount) credits the amount to the account.
Note: You may assume that the amount parameter passed is always positive or 0
The locked stub code validates the correctness of the Account class implementation by performing the
following operations:
Debit amount: This operation debits the amount. If return value is false, it prints ‘insufficient
balance’. Otherwise it prints ‘<amount> debited’
Credit amount: This operation credits the amount and prints ‘<amount> credited’
GetBalance: This operation gets the current balance and prints ‘Current balance is <balance>’
The first line contains an integer that denotes the initial balance when the Account object is created. The
second line contains an integer n, the number of operations to be performed. Each line i of the n
subsequent lines (where 0 ≤ i ≤ n) contains one of the three operations listed above and their
parameters, if any.
Sample Case 0
Sample Input
Credit 1000
GetBalance
Sample Output
1000 debited
1000 credited
Current balance is 1000
Explanation
An account object is created with an initial balance of 1000. The debit operation is performed
debit(1000) which returns true, followed by a credit operation credit(1000). Finally, current balance is
called.
Sample Case 1
Sample Input
10000
Debit 100000
Credit 1000
GetBalance
Sample Output
Insufficient Balance
1000 credited
For example calling dataFinder([1,6,3,0,2,15,10]) must return a function find such that calling
find(2,4,10) returns false. It searches for 10 in the inclusive range 2 through 4, the subarray [3,0,2]. It is
not found in the given range as it lies at index 6. So the function returns false.
The implementation will be tested by a provided code stub using several input files with parameters.
The dataFinder function will be called with the data parameter, and the returned function will be called
with the minRange, maxRange and value parameters. The result will be printed to the standard output
by the provided code.
Constraints:
The first line contains space-separated integers, the integers in the data array. The second line contains
space-separated integers, the minRange, maxRange and value respectively.
Sample Case 0
Sample Input
data = [15,1,10,5,4,20]
Sample output
true
Explanation
The call dataFinder([15,1,10,5,4,20]) returns a function which is called with find(1,4,4). It returns true, 5
is in the range (1-4) at index 4
Sample Case 1
Sample Input
10 1 0 13 4 15
1 10 13
Sample Output
Code Snippet:
3. Fizz Buzz
Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as
follows:
Function Description
Returns: None
Prints: The function must print the appropriate response for each value, i in the set {1,2, … n} in
ascending order, each on a separate line
Constrains
Sample Case
Sample Input
n = 15
Sample Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Explanation
The number 3, 6, 9, and 12 are multiple of 3 (but not 5), so print Fizz on those lines. The number 5 and
10 are multiple of 5 (but not 3), so print Fizz on those lines. The number 5 and 10 are multiples of 5 (but
not 3), so print Buzz on those lines. The number 15 is a multiple of both 3 and 5, so print FizzBuzz on that
line. None of the other values is a multiple of either 3 or 5, so print the value of i on those lines.
4. Joined Logger
In this challenge, each message object has two properties:
For example:
Calling joinedLogger(15, ‘;’) must return a function f, such that calling f(msg1, msg2, msg3) causes the
logger to write the string “bar;baz” to the defined output. The level passed to the joinedLogger is 15 and
the separator is ‘;’ Only msg2 and msg3 have a level greater than or equal to 15, so the text of those
messages “bar” and “baz” is joined with the ‘;’ separator and written to the defined output by the
logger.
Sample case 0
Sample Input
Sample Output
foo;baz;bax
Explanation
The first, the second and fourth message have levels greater than or equal to 21. Their values, joined
with ‘;’ are written by the logger to the defined output.
Sample Case 1
Input
2 -> n = 2
Output
item1-item2
5. Notes Store
In this challenge, the task is to create a class NotesStore. The class will manage a collection of notes,
with each note having a state and a name. Valid states for notes are: ‘completed’, ‘active’ and ‘others’.
All other states are invalid.
1. addNotes(state, name): adds a note with the given name and state to the collection. In addition to
that:
If the passed name is empty, then it throws an Error with the message ‘Name cannot be empty’.
If the passed name is non-empty but the given state is not a valid state for a note, then it
throws an Error with the message ‘Invalid state {state}’.
2. getNotes(state): returns an array of names of notes with the given state added so far. The names are
returned in the order the corresponding notes were added. In addition to that:
If the given state is not a valid note state, then it throws an Error with the message ‘invalid state
{state}’
If no note is found in this state, it returns an empty array.
Your implementation of the function will be tested by a stubbed code on several input files. Each input
file contains parameters for the function call. The function will be called with those parameters, and the
result of their executions will be printed to the standard output by the provided code. The stubbed code
joins the strings returned by the getNotes function with a comma and prints to the standard output. If
getNotes returns an empty array, the stubbed code prints ‘No Notes’. The stubbed code also prints
messages of all the thrown errors.
In the first line, there is an integer n, denoting the number of operations to be performed. Each line i of
the n subsequent lines (where 0 ≤ i < n) contains space separated string such that the first of them is a
function name, and the remaining ones, if any, are parameters for that function.
Sample Case 0
getNotes active
getNotes completed
getNotes foo
Sample Output
DrinkTea, DrinkCoffee
Study
Explanation
For all 3 addNote operation, the addNote function is called with a state and a name. Then, the getNotes
function is called for ‘active’ state and ‘completed’ state respectively, and the result is printed. Then
getNotes function is called for ‘foo’ state, which throws an error since this state is invalid, and the error
is printed.
6. Staff List
The task is to create a class StaffList. The class will manage a collection of staff members, where each
member is uniquely identify by a name. The class must have the following methods:
1. add(name, age):
Parameters string name and integer age are passed to the function
If age is greater than 20, it adds the member with the given name to the collection
Else if age is less than or equal to 20, it throws an Error with the message ‘Staff member age
must be greater than 20’.
It is guaranteed that at any time, if a member is in the collection, then no other member with
the same name will be added to the collection
2. remove(name):
If the member with the given name is in the collection, it removes the member from the
collection and returns true
Else if the member with the given name is not in the collection, it does nothing and returns
false.
3. getSize():
Returns the number of members in the collection
Your implementation of the class will be tested by a stubbed code on several input files. Each input file
contains parameters for the function calls. The functions will be called with those parameters, and the
result of those executions will be printed to the standard output by the provided code. The stubbed
code prints values returned by the remove(name) and getSize() functions, and it also print messages of
all the cached errors.
Sample Case 0:
Sample Input:
add John 25
add Robin 23
getSize
remove Robin
getSize
Sample Output:
True
Explanation: There are 2 staff members, ‘John’ and ‘Robin’, who are added by calling the add function
twice. getSize is then called and returns the numbers of members in the collection, which is 2. Then the
staff member ‘Robin’ is removed from the list by calling the remove function, and since the given name
is in the collection, the return value true is printed. Finally, getSize is called, which prints the size of the
collect, which is now 1 because ‘Robin’ was removed
Sample Case 1:
Sample Input:
add John 20
add Robin 10
getSize
remove Robin
getSize
Sample Output:
Error: Staff member age must be greater than 20
false
7. Step Counter
In this challenge, you are provided with the implementation of a simple counter object:
const counter = (function counter() {
let value = 0;
return {
getValue: function(){
return value;
},
changeBy: function(k) {
value += k;
},
}
})();
Your implementation must encapsulate the provided counter object and use it for its implementation.
The object returned by stepCounter must not have a changeBy property.
Your implementation of the function will be tested by a provided code stub on several input files. Each
input file contains a parameter for stepCounter, followed by several values denoting the operations to
perform on the object return by stepCounter. The results of performing the operations will be printed to
the standard output by the provided code.
In the first line, there is a single integer k denoting the parameter for the stepCounter function. In the
second line, there is integer n, denoting the number of operations to perform. Next n lines follow. Each
of them contains a single character, either + or – or ? denoting respectively calling increment(),
decrement() and getValue() methods on the object returned by stepCounter.
Sample Case 0
+ -> increment()
? -> getValue()
- -> decrement()
? -> getValue()
Sample Output
Explanation
In this test, the k parameter for stepCounter is 1, so each increment must increment the value by 1 and
each decrement must decrement the by 1. Initially, the counter has the value 0. There are 4 operations
to be performed. The first of them increments the counter, so it has the value 1 now. The second prints
the current value of the counter. The third decrements the counter so it has the value 0 now. The fourth
prints the current value of the counter
Sample case 1
- -> decrement()
? -> getValue()
+ -> increment()
+ -> increment()
? -> getValue()
Sample output
-2
2
8. Strip Property
In this challenge, the task is to implement a function stripProperty that:
Sample Input
STDIN Function
Sample Output
bar 3
baz 3
Explanation
In this test, obj has 3 properties: foo, bar and baz. The property to strip is foo, so the return obj literal
contains properties bar and baz from the obj with the same values, but it doesn’t contain the property
foo.
GitHub Solutions
Follow:- Sazzad-Saju
Repo:- HackerRank-Solutions
(END)