NodeJS Slip 14B solution

//Slip_14B.js

You might have files for handling input (app.js), calculating bills (billCalculator.js), and possibly for storing and retrieving data.

//app.js
const readline = require('readline');
const { calculateBill } = require('./billCalculator');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question('Enter the number of units consumed: ', (units) => {
const billAmount = calculateBill(units);
console.log('Your electricity bill amount is: $${billAmount.toFixed(2)}');
rl.close();
});

//billCalculator.js

function calculateBill(units) {
let billAmount = 0;
if (units <= 100) {
billAmount = units * 1.50; // Replace 1.50 with your applicable rate
} else if (units <= 200) {
billAmount = 100 * 1.50 + (units - 100) * 2.00; // First 100 units at 1.50, remaining at 2.00
} else if (units <= 300) {
billAmount = 100 * 1.50 + 100 * 2.00 + (units - 200) * 2.50; // First 100 units at 1.50, next 100 at 2.00, remaining at 2.50
} else {
billAmount = 100 * 1.50 + 100 * 2.00 + 100 * 2.50 + (units - 300) * 3.00; // First 100 units at 1.50, next 100 at 2.00, next 100 at 2.50, remaining at 3.00
}
return billAmount;
}

module.exports = { calculateBill };

No comments:

Post a Comment