Problem 4. monthlybilling.js(.split is introduced)

 /*

Given the unit price, number of units per day and
the number of days in a given month,
compute the monthly bill.
Input:
one line containing 3 integers seperated by spaces.
First integer denotes the unit price.
Second integet denotes the no. of units per day.
Third integer denotes the no. of days in a month.

Output:
one integer denoting the monthly bill.
Example:
Input:-
5.2 2 30
20 2 30

Expected output:
312
600

Algorithm:
1. Read the price per unit
2. Read the no. of units per day
3. Read the no. of days in a month
4. Apply or calculate the final bill
5. Display final amount
*/
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 tempVariable = readLine(); // "5.2" "2" "30"
tempVariable = tempVariable.split(" "); // ['5.2', '2', '30']
let unitPrice = parseFloat(tempVariable[0]); // '5.2'--> 5.2 (number)
let unitsPerDay = parseFloat(tempVariable[1]); // '2'---> 2
let noOfDays = parseInt(tempVariable[2]); // '30'----> 30
let finalBill = unitPrice * unitsPerDay * noOfDays; // 5.2*2*30

console.log(finalBill); //312

Terminal:-
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node monthlybilling.js <input.txt 312

Comments

Popular posts from this blog

Problem 33. patter122333.js

Problem 38. diamond.js

Problem 16. chocolatebills.js