Compare variable values and choose larger ones to calculate

Asked

Viewed 383 times

0

I am developing a retirement calculator. Available at link http://www.utilizaweb.com.br/aposentadoria (code also available below). The program works with the insertion of several companies. I need that when the user gives the entry of for example: (for male user)

If the user worked in the first company, by the rule of 25 years, the time of 6 years; If the user worked in the second company, by the rule of 25 years, the time of 2 years; If the user worked in the third company, by the rule of 15 years, the time of 4 years; If the user worked in the fourth company, by the rule of 20 years, the time of 3 years;

The program should take the larger rules, in the case of the two 25-year-olds, add the time worked in the two, multiply by 1.4 and subtract by 35.

I wanted to know now how I do to compare in all fields, what are the rules, take the largest that are equal, add up the amount of time worked and continue the account. Thank you very much :)

html

<div class="container">
<h3><span>Saiba quando você irá se aposentar!</span><br />Preencha suas informações abaixo!</h3>
<form class="form-calculadora" name="aposentadoria">

    <fieldset class="sexo">
        <legend>Sexo</legend>
        <select id="sexo" name="sexo" class="text-area">
            <option value="M"><i class="fa fa-male"></i>Masculino</option>
            <option value="F"><i class="fa fa-female"></i>Feminino</option>
        </select>
    </fieldset>

    <fieldset class="trabalho">
        <div class="row" id="p_scents">
                            <div class="empresa-area">
                <p class="title">Empresa</p>
                <input type="text" name="empresa" class="text-area" placeholder="Empresa: ">
            </div>

            <div class="regra-area">
                <p class="title">Regra</p>
                <select name="regra" class="text-area">
                    <option value="25">25 anos</option>
                    <option value="20">20 anos</option>
                    <option value="15">15 anos</option>
                </select>
            </div>

            <div class="admissao-area">
                <p class="title">Admissão</p>
                <input type="date" name="dataAdmissao" class="text-area" placeholder="Admissão: ">
            </div>

            <div class="demissao-area">
                <p class="title">Demissão</p>
                <input type="date" name="dataDemissao" class="text-area" placeholder="Demissão: ">
            </div>

            <div class="anos-area">
                <p id="anosTrabalhados">0 Anos</p>
            </div>
        </div>


    </fieldset>

    <p class="add-empresa"><a href="#" id="addScnt"><i class="fa fa-user-plus"></i> Adicionar Empresa</a></p>

    <input type="button" value="Calcular" onclick="calcula();" >

</form>

<p id="result"><img src="./img/aviso.png"></p>

Javascript

function calcula(){
var sexo = document.aposentadoria.sexo.value;
var nRegra = document.aposentadoria.regra.value;
var empresa = document.aposentadoria.empresa.value;
var dataAdmissao = new Date(document.aposentadoria.dataAdmissao.value);
var dataDemissao = new Date(document.aposentadoria.dataDemissao.value);

if (dataDemissao > dataAdmissao) {;
    var anosTrab = dataDemissao.getFullYear() - dataAdmissao.getFullYear();
    var dateTrab = dataDemissao.getDate() - dataAdmissao.getDate();
    var mesTrab = dataDemissao.getMonth() - dataAdmissao.getMonth();
} else {
    alert("A data de admissão deve ser anterior à data de demissão.");
}

if ((sexo == "M") && (nRegra == "25")) {
    anosNovo = anosTrab * 1.40;
} else if ((sexo == "M") && (nRegra == "20")) {
    anosNovo = anosTrab * 1.75;
} else if ((sexo == "M") && (nRegra == "15")) {
    anosNovo = anosTrab * 2.33;
} else if ((sexo == "F") && (nRegra == "25")) {
    anosNovo = anosTrab * 1.20;
} else if ((sexo == "F") && (nRegra == "20")) {
    anosNovo = anosTrab * 1.50;
} else if ((sexo == "F") && (nRegra == "15")) {
    anosNovo = anosTrab * 2;
}


if (sexo == "M") {
    anosFalta = anosNovo - 35;
} else if (sexo == "F") {
    anosFalta = anosNovo - 30;
}

if (anosFalta < 0) {
    anosFim = anosFalta * -1;
}

var anosAjus = "Você trabalhou " +anosTrab+ " anos " +mesTrab+ " meses e " +dateTrab+ " dias";
var calculoNovaR = "Faltam " +anosFim+ " anos para se aposentar";

document.getElementById("anosTrabalhados").innerHTML = anosTrab+ " anos";
document.getElementById("result").innerHTML = "<img src='./img/aviso.png'>" +anosAjus+ "<br />" +calculoNovaR;}

Jquery for ADD more fields

$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;

$('#addScnt').live('click', function() {
    $('<div class="row"><div class="empresa-area"><input type="text" name="empresa' + i +'" class="text-area" placeholder="Empresa: "></div><div class="regra-area"><select name="regra' + i +'" class="text-area"><option value="25">25 anos</option><option value="20">20 anos</option><option value="15">15 anos</option></select></div><div class="admissao-area"><input type="date" name="dataAdmissao' + i +'" class="text-area" placeholder="Admissão: "></div><div class="demissao-area"><input type="date" name="dataDemissao' + i +'" class="text-area" placeholder="Demissão: "></div><div class="anos-area"><p id="anosTrabalhados">0 Anos</p></div></div>').appendTo(scntDiv);
                i++;
                return false;
});

$('#remScnt').live('click', function() { 
    if( i > 2 ) {
        $(this).parents('p').remove();
        i--;
    }
    return false;
});

});

  • Hello. It is possible to put the example in jsfiddle. It is easier to help.

  • In the meantime the solution must go through: No "calculate" go through all the records in this case $('.Row'). each(); within that each should do the calculation and save the values for then at the end know which one you want to.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.