Problem 13. Ivrs.js (while loop is introduced) lecture 6 stoc module

 /*Question

Chillkart is a new online shopping website and wants to build a Ivrs.
The Ivrs has the following option:
Option 1 - know the details of recent order.
Option 2 - know about today's discount offer.
Option 3 - know the balance in your account.
0 - exit

Chillkart has data of various callers. for each call Chillkart wants to count
how many times each option was choosen.

Input:
Each line containing one integer denoting the customer's choice.
Stop processing when 0 is encountered.

Output:
3 lines containing one integer each.
First line should contain the total number of times the customer choose option 1.
Second line should contain the total number of times the customer choose option 2.
Third line should contain the total number of times the customer choose option 3.

Example:
Input:
2
3
0

1
1
1
0

Expected output:
0--> no of times option 1 was choosen
1--> no of times option 2 was choosen
1--> no of times option 3 was choosen

3
0
0

Algorithm:
1. Read the customers choice
2. If choice is 0 exit
3. If any other option is choosen, increment that option variable
4. Ask to enter again
*/

let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
//..............................................................................................................................

let option1 = 0;
let option2 = 0;
let option3 = 0;

let choice = parseInt(readLine());// if choice is 0 exit or else go into the loop

while (choice != 0) {
if (choice == 1) {
option1++;

}

else if (choice == 2) {
option2++;
}

else if (choice == 3) {
option3++;
}

else {
console.log("only press 1, 2 or 3")
}

choice = parseInt(readLine()); // this is enter again
}

console.log(option1);
console.log(option2);
console.log(option3);

Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node Ivrs.js <input.txt 0 1 1 Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
-----------------------------------------------------------------------------------------------



second Method:

let option1 = 0;
let option2 = 0;
let option3 = 0;
let choice;

while ((choice = parseInt(readLine())) != 0) {
if (choice == 1) {
option1++;

}

else if (choice == 2) {
option2++;
}

else if (choice == 3) {
option3++;
}

else {
console.log("only press 1, 2 or 3")
}

}

console.log(option1);
console.log(option2);
console.log(option3);


==============================================================================================

Third Method Using forloop:
let option1 = 0;
let option2 = 0;
let option3 = 0;
let choice;

for (; (choice = parseInt(readLine())) != 0;) {
if (choice == 1) {
option1++;

}

else if (choice == 2) {
option2++;
}

else if (choice == 3) {
option3++;
}

else {
console.log("only press 1, 2 or 3")
}

}

console.log(option1);
console.log(option2);
console.log(option3);









Comments

Popular posts from this blog

Problem 33. patter122333.js

Problem 38. diamond.js

Problem 16. chocolatebills.js