Problem 24. election.js ( continue is used)
/* Question
Election commision received application for voter ids.
A person is eligible only if their age is more than 18 years.
Help election commision to count number of valid application
Input:
First line containing a positive integer , say n , denoting the number of application.
Next n line contains one integer , denoting the number of valid applicants.
Output:
one line containing number of valid applicants.
Example:
Input:
5
23
45
18
20
17
Output:
3
Algorithm:
1. Read the number of applicants.
2. Read the age of the applicants and see if greater than 18 years.
3. If yes increment the value, else continue.
4. Display the count.
*/
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 noOfApplicants = parseInt(readLine()); //5
let validApplicants = 0; // This is the count variable, we assumung it to be zero now
let i = 0; // initialzation for while loop
while (i < noOfApplicants) {
let age = parseInt(readLine());
if (age > 18) {
validApplicants++
}
i++
}
console.log(validApplicants);
OR
==============================================================================================
By using continue keyword:-
let noOfApplicants = parseInt(readLine()); //5
let validApplicants = 0; // This is the count variable, we assumung it to be zero now
for (i = 0; i < noOfApplicants; i++) {
let age = parseInt(readLine());
if (age <= 18) {
continue; // if age is <= 18 skip that iteration with the help of continue keyword
}
validApplicants++;
}
console.log(validApplicants);
==============================================================================================
Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node election.js <input.txt
3
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment