mathCeil.js ( Math.ceil method is introduced)

 /**

* Emergency cess
Due to a natural disaster, the government is exploring various ways to gather relief fund. One proposal is to collect a one time cess from salaried employees. As per the proposal, everyone should pay ceil(7% of their monthly salary).

Given a the number of employees and their monthly salaries, help the government find the fund that can be gathered in a month.

Write a function to do this calculation.

Input
First line contains an integer n, denoting the number of employees. Next line contains n space seperated integers, denoting the monthly salaries of the n employees.

Output
One Integer, denoting the result.

Example
Sample Input:

5
100 200 300 400 555
Sample Output:

109
Explanation
First line is 5, which denotes the length of input n.

100 200 300 400 555 denotes the monthly salaries of these n people.

As per the rule,

ceil(7% of 1st person monthly salary) is 7

ceil(7% of 2nd person monthly salary) is 14

ceil(7% of 3rd person monthly salary) is 21

ceil(7% of 4th person monthly salary) is 28

ceil(7% of 5th person monthly salary) is 39

Resultant sum will be 7+14+21+28+39 = 109

*/

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];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

const calculateTotalFund = function (salaries, peopleCount) {

let sum = 0;

for (i = 0; i < peopleCount; i++) {
// 0) i=0,
// 1) i=1,
// 2) i=2,
// 3) i=3,
// 4) i=4

sum = sum + Math.ceil((salaries[i] * 7) / 100)
//0) 0+100*7/100=7,
//1) 7+200*7/100=21,
//2) 21+300*7/100=42,
//3) 42+400*7/100=70,
//4) 70+555*7/100=108.85 ( by math.ceil it will be rounded up to 109)

}

return sum
}


// This part is pure input taking and was already given in the question

function ConvertToNumber(list) {
for (let each in list) {
list[each] = Number(list[each])
}
}

let peopleCount = parseInt(readLine()); // 5
let salaries = readLine().split(" ");// ["100","200","300","400","555"]
ConvertToNumber(salaries);
let totalFund = calculateTotalFund(salaries, peopleCount);// Invoking the function calculate total fund
console.log(totalFund);

//////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:





Comments

Popular posts from this blog

car.js

movie.js