-1

I'm looking a solution but can't find a good explanation. I writing a code that I can easily change strings or others parameters. What I want is to pass values to a string when I call it via function, Something like this...

Head Of document:

$(document).ready(function(){
 $('.DRs').mask("#.###0.00", {reverse: true});
 $('.NewR').mask("#.###0.00", {reverse: true});
//Modificaciones Directas a Dropdowns
$("select").css("fontSize","13.5px");   //Tamaño de la fuente
$("select").css("width","70%");         //Anchura
//$("select").css("text-align","center");   //Texto centrado

$(".Inst").css("text-align","left");
$(".Inst").css("width","80%");
$(".Inst").css("fontSize","50.5px"); 
ck1="";
Codigo="";
Tipo="";

//Arreglos
Segmentos = ['-Seleccione-','Caja Seguro Social', 'Contraloria'];                                       //Agregar aqui en caso nuevos Segmentos.
Tipo = ['-Seleccione-','Ahorro','Aportación','Crédito','Cuotas','Embargo','Hipoteca','PP','SIACAP','Sindicato'];    //Listado de tipo
Alertas =[];
MostrarCuadroAlerta = 0;

//Strings de Alertas
Alerta_Ahorro = "Alerta: " + Tipo.value + " sin evidencia, con letra mensual de: " + ck1.value + "Bajo el codigo: " + Codigo.value; 
Alerta_Carta_R50 = "Alerta: Codigo "+ Codigo.value +" Se solicita buscar Carta de ASOCIACION NACIONAL DE MUEBLERIAS"; 

    //Agregando regla de Ahorro
    AhorroBlock();
    //Agregando Valores a las Listas Desplegables
    fillDropDwn();

 });

and in the function...

 function MSh() {
    var x, y, z, t, h, p;
    var SumTst;
    var FirstLetter;
    var SumTst = 0;
    var tabla = document.getElementById("Registros");
    var len = document.getElementById("Registros").rows.length;


    ////Restando Deducibles al 35%
    var SumaDeducciones = 0;
    var MenosIR = document.getElementById("DEDUIMR");
    var MenosEdu = document.getElementById("DEDUEDU");
    var MenosSS = document.getElementById("DEDUSS");
    var MenosOtrs = document.getElementById("DEDUOTRS");
    var get_trecinco = document.getElementById("DI2");
    var TMP = document.getElementById("DI1");

    //alert("Valores insertados:" + " " + MenosIR.value +" "+ MenosEdu.value +" "+ MenosSS.value + " "+MenosOtrs.value);
    //Sumando deducciones

    SumaDeducciones = TMP.value - (Number(MenosIR.value) + Number(MenosEdu.value) + Number(MenosSS.value) + Number(MenosOtrs.value));
    //SumaDeducciones =  TMP.value - 3 ;
    SumaDeducciones = Number(SumaDeducciones);
    //alert(SumaDeducciones);
    //Restando Deducciones al 35%
    var TotalDeducciones = document.getElementById("DI1").value = SumaDeducciones.toFixed(2);
    //alert (TotalDeducciones);
    //Beggining Loop


    for (y = 1; y < (len); y++) {

        for (x = 0; x <= 2; x++) {
            //Extraccion de la primera letra del codigo.
            if (x == 0) {
                //Codigo
                try {
                    Codigo = tabla.rows[y].cells[x].getElementsByClassName('CLPMRY')[0];
                    //CHECKLETTER = tabla.rows[y].cells[x].getElementsByClassName('CLPMRY')[0];  
                } catch (err) {
                    alert(err.message);
                }
                //VALIDANDO LETRA H EN LOS CODIGOS 
                Letra = Codigo.value.charAt(0);
                alert(Letra);

            }

            if (x == 2) {
                //Llevar esto una función. Limpiaria un poco la pantalla del proceso. 

                ck1 = tabla.rows[y].cells[x].getElementsByClassName('NewR')[0]; //Monto Mensual
                EvidenciaChck = tabla.rows[y].cells[4].getElementsByClassName('EvidenciaS')[0]; //Evidencia
                Tipo = tabla.rows[y].cells[1].getElementsByClassName('Tpo')[0]; //Tipo Descuento
                SeCancelo = tabla.rows[y].cells[3].getElementsByClassName('Cncel')[0]; //Cancelación

                if (ck1.value > 0 && Tipo != "" || Tipo != "-Seleccione-" && Codigo != "") {
                    cero = document.getElementById("DI1");

                    // E

                    if (Letra == "E") {
                        alert("AHORRO");
                        Tipo.value = "Ahorro";

                    }

                    //---------------------------------------------
                    //Regla# 1- Segun reglas: Si el codigo comienza con H. Se resta del 75% el doble del descuendo de dicho codigo.
                    //--------------------------------------------
                    if (Letra == "H" || Letra == "h") {
                        var toal = cero.value - (ck1.value * 2);
                        alert("H");
                        var toalF = toal.toFixed(2);
                        document.getElementById("DI1").value = toalF;
                    }

                    //---------------------------------------------
                    //Regla # 2 - Los ahorros y aprotaciones mayores a $25.00 si no tienen evidencia, afectan 35% y siempre afectan 75% 
                    //---------------------------------------------

                    if (EvidenciaChck.value == "No" && ck1.value >= 25.00 && Tipo.value == "Ahorro") {
                        var Tycinco = document.getElementById("DI2");
                        var Sycinco = document.getElementById("DI1");
                        var toal1 = Tycinco.value - ck1.value;
                        var toal2 = Sycinco.value - ck1.value;

                        document.getElementById("DI2").value = toal1.toFixed(2);
                        document.getElementById("DI1").value = toal2.toFixed(2);

                        Alertas.push("Alerta: " + Tipo.value + " sin evidencia, con letra mensual de: " + ck1.value + " Bajo el codigo: " + Codigo.value); //Parametros
                        MostrarCuadroAlerta = 1; //Activando Cuadro de Alertas
                    }

But for some reason when I print the Array the values comes with "Undifined".

Im looking something like this.

    Alerta_Ahorro = "Alerta: " + {1} +" sin evidencia, con letra mensual de: " + {2} + "Bajo el codigo: " + {3}; 

    Alert.push(Alerta_Ahorro,Tipo.value,ck1.value,Codigo.value)
  • 1
    Did you make those vars before or you just add somevar = something; ? (Did you add before this code like var Alertas, .........;) –  Mar 28 '18 at 19:26
  • *"What I want is to pass values to a string when I call it via function"* how about using a function? – Thomas Mar 28 '18 at 19:28
  • 3
    This code is written so unclean way I can't even read it.... –  Mar 28 '18 at 19:29
  • You have inconsistent indentation, use of space around operators (e.g. =, +), no variable declarations anywhere, uncompleted functions (missing closing brace) there's no way anybody can *read* this well enough to help you, it's a mess even after I edited it. I can't even tell what your question is. Please update with a minimal, complete, verifiable example with sample input and expected output. – Jared Smith Mar 28 '18 at 19:33
  • Possible duplicate of [JavaScript equivalent to printf/string.format](https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format) – AP. Mar 28 '18 at 19:34
  • @AP. I can't make heads or tails of this enough to tell if it's a dupe of the other (which is a problem in it's own right). – Jared Smith Mar 28 '18 at 19:35
  • @JaredSmith I agree, but it seems like that's basically what OP's gunning for, given the last stanza – AP. Mar 28 '18 at 19:36
  • Sorry, Tried to only add the relevant code. The entire code is way larger. Basically in the head of the Document I want a string glossary that i can Edit easily. But the variables are coming from a dynamic and very complex Table. Is a calculator for a Bank where I work. But I think the Problem is clear... I used to write codes in C# and connections with MYSQL and I remember that I can add parameters to a String. That's exactly I want to do with my code. – Josh Gomez Mar 29 '18 at 14:35

1 Answers1

2

You can use template literals (except for in IE):

Alerta_Ahorro = `Alerta: ${Tipo.value} sin evidencia, 
    con letra mensual de: ${ck1.value} 
    Bajo el codigo: ${Codigo.value}`;
sliptype
  • 2,804
  • 1
  • 15
  • 26