'I'm trying to calculate salary in JS where there are two different rates

I'm trying to calculate salary where there are two different rates within the same time range:from 10:00-15:00 the rate is 20and from 15:00 to the end the rate is 15. Link for the whole code: https://replit.com/@barel31/tenbis-calculator#src/App.jsx So for example if I work from 11:00-16:30 it should calculate the first 4 hours by 20 and the rest by 15 so 4x20 + 1.5x15 = 102.5

const calc = () => {
    var hour = 30;
    hour += vehicle === 'Motorcycle' ? 15 : 10;

    const timeStartDec = hoursToDec(timeStart);
    const timeEndDec = hoursToDec(timeEnd);

    const hours = timeEndDec - timeStartDec;

    if (timeStartDec >= 10 && timeEndDec <= 15) {
        // Here are the condition if the shift start before 10:00 and and end before 15:00
        // but the problem is when it won't calculate if the user start after 10:00 or end before 15:00 
        hour += 5;
    }

    const minutes = hours / 60;
    for (var i = 0; i < minutes; i++) {
        break;
    }

    return (
        <>
            <p>hours: {hours}</p>
            <p>per hour: {hour}</p>
            <p>wage: {hours * hour}</p>
            <p>dec start: {timeStartDec}</p>
            <p>dec end: {timeEndDec}</p>
            <p>wage: {hours * hour + (tips | 0)}</p>
        </>
    );
};


Solution 1:[1]

It is notable that there are six different cases in this problem.

  • starttime < timeRange[0] and stoptime < timeRange[0] (e.g. - timeRange = ['10.00', '12.00'],starttime='8.00' and stoptime = '9.00')
  • starttime < timeRange[0] and timeRange[0]< stoptime < timeRange[1] (e.g. - timeRange = ['10.00', '12.00'],starttime='8.00' and stoptime = '11.00')
  • timeRange[0] < starttime < timeRange[1] and timeRange[0] < stoptime < timeRange[1] (e.g. - timeRange = ['10.00', '12.00'],starttime='11.00' and stoptime = '11.30')
  • timeRange[0] < starttime < timeRange[1] and stoptime > timeRange[1] (e.g. - timeRange = ['10.00', '12.00'],starttime='11.00' and stoptime = '13.00')
  • starttime > timeRange[1] and stoptime > timeRange[1] (e.g. - timeRange = ['10.00', '12.00'],starttime='13.00' and stoptime = '14.00')
  • starttime < timeRange[0] and stoptime > timeRange[1] (e.g. - timeRange = ['10.00', '12.00'],starttime='9.00' and stoptime = '14.00')

So we need to calculate the problem for these six cases.

The easiest way of doing this is getting the hours and comparing them and generating the output.

function calculate(starttime, stoptime){
    var timeRange = ['10.00', '12.00']
    var payRange = [15,20]
    var ss1 = getHours(starttime, timeRange[0])
    var ss2 = getHours(starttime, timeRange[1])
    var st1 = getHours(stoptime, timeRange[0])
    var st2 = getHours(stoptime, timeRange[1])
    var p = 0
    //case 1 
    if(ss1>=0 && ss2>=0 && st1>=0 && st2>=0){
        p = (ss1-st1) * payRange[0]
    }
    //case 2 
    if(ss1>=0 && ss2>=0 && st1<=0 && st2>=0){
        p = ss1 * payRange[0] + st2 * payRange[1]
    }
    //case 3
    if(ss1<=0 && ss2>=0 && st1<=0 && st2>=0){
        p = (ss2-st2) * payRange[1]
    }
    //case 4
    if(ss1<=0 && ss2>=0 && st2<=0 && st1<=0){
        p = ss2 * payRange[1] - st2 * payRange[0]
    }
    //case 5
    if(ss1<=0 && ss2<=0 && st1<=0 && st2<=0){
        p = (ss2-st2) * payRange[0]
    }
    //case 6
    if(ss1>=0 && ss2>=0 && st1<=0 && st2<=0){
        p = ss1 * payRange[0] + (ss2-ss1) * payRange[1] - st2 * payRange[0]
    }  
}

function getHours(time1, time2){
    var t1 = time1.split(".")[0]*60 + time1.split(".")[1]*1;
    var t2 = time2.split(".")[0]*60 + time2.split(".")[1]*1;
    return (t2-t1)/60
}

This is the easiest way to do this without thinking about optimizing the solution. Hope you will get an idea from this on how to implement your code. And since I am using <= and >= operators the time range inclusions in startime and stoptime won't work. Hope you'll get some insight from this and be able to solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nipuna Upeksha