- Published on
Tally Counter Problem Implementation in JavaScript
- Authors
- Name
Counter Class Implementation
Problem Statement
You need to implement a class called Counter
that manages a private integer variable tally
. The variable tally
is initialized to 10 and should not be directly accessible from an instance of Counter
. The class should expose two methods:
increment(delta)
: Increments the value oftally
bydelta
.getTally()
: Returns the current value oftally
.
You will perform m
operations on an instance of the Counter
class. The operations are of two types:
1 delta
: Increment the value of the private variabletally
bydelta
.2
: Return the value of the private variabletally
.
Input Format
- The first line contains an integer
m
, the number of operations. - The next
m
lines contain either:1 delta
to increment the tally bydelta
.2
to return the current value oftally
.
Output Format
For each operation of type 2
, output the current value of tally
on a new line.
Constraints
1 <= m <= 200
1 <= delta <= 5000
Sample Input
3 1 5 1 6 2
Sample Output
21
Explanation
- The first operation
1 5
increments thetally
from 10 to 15. - The second operation
1 6
increments thetally
from 15 to 21. - The third operation
2
returns the current value oftally
, which is 21.
Solution
class Counter {
#tally = 10; // private variable, initialized to 10
increment(delta) {
this.#tally += delta;
}
getTally() {
return this.#tally;
}
}
// Read input from stdin
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputString = '';
let inputArray = [];
let currentLine = 0;
rl.on('line', (inputStdin) => {
inputString += inputStdin + '\n';
});
rl.on('close', () => {
inputArray = inputString.trim().split('\n');
main();
});
function readLine() {
return inputArray[currentLine++];
}
function main() {
const m = parseInt(readLine());
const counter = new Counter();
for (let i = 0; i < m; i++) {
const input = readLine().split(' ');
const type = parseInt(input[0]);
if (type === 1) {
const delta = parseInt(input[1]);
counter.increment(delta);
} else if (type === 2) {
console.log(counter.getTally());
}
}
}
Further Explanation Class Definition:
The Counter class has a private variable #tally initialized to 10. The # symbol indicates that tally is a private field and cannot be accessed outside of the class. The increment(delta) method increments the value of tally by delta. The getTally() method returns the current value of tally. Input Handling:
The readline module is used to read input from stdin. The inputString stores the entire input. The inputArray splits the input into individual lines. The readLine() function returns the next line from inputArray. Main Logic:
The number of operations m is read. A new instance of Counter is created. For each operation, it checks the type: If the type is 1, it increments the tally by the given delta. If the type is 2, it prints the current value of tally. This implementation ensures that the tally variable is properly managed and manipulated according to the given operations.
Comments
Loading comments...