Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
Javascript function not returning dictionary which it should be

I currently have two javascript functions which both produce the desired result alone, however when together do not.

The first function is process results. All this does is log the result of calling another function, textToTime. The text to time function accepts a dictionary and returns another dictionary, with a string time converted to an integer time.

For example,

textToTime({A: "32 min", B: "1 hour 10 min" C: "2 hours 1 min"})

should return

{A: 32, B: 70, C: 121}

Here are the following functions:

async function processResults(options) {
    var bestOption = "";
    var display = false;
    options = await textToTime(options);
    console.log(options);
}
function textToTime(options) {
    for (var key in options) {
        let total = 0;
        let current = options[key];
        if (current == "n/a") {
            total = -1;
        }
        else {
            var num = "";
            for (let i = 0; i < current.length; i++) {
                if (isNumeric(current.charAt(i))) {
                    num += current.charAt(i);
                }
                else {
                    if (current.charAt(i) == "h") {
                        total += 60 * parseInt(num);
                        num = "";
                    } 
                    if (current.charAt(i) == "m") {
                        total += parseInt(num);
                        num = "";
                    }
                }
            }
        }
        options[key] = total;
    }
    return options;
}

When I run textToTime alone, it always produces the correct output, however when I call it from inside processResults, it tends to return only part of the dictionary with integers and the rest still in the string format like: {A: 32, B: "1 hour 10 min", C: "2 hours 1 min"}

Any knowledge or support is greatly appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

in processResults function you have options argument , then you pass options to textToTime function and you assign the result to the options !!!!

this is not correct! change the last options to res or result or apiResult so that you avoid confusion! it also may result in unexpected behavior and cause errors

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code is working fine just use $.isNumeric() method instead of isNumeric() becuase of this is not defined method.

Helpful Link: https://api.jquery.com/jquery.isnumeric/

async function processResults(options) {
    var bestOption = "";
    var display = false;
    options = await textToTime(options);
    console.log(options);
}

function textToTime(options) {
    for (var key in options) {
        let total = 0;
        let current = options[key];
        if (current == "n/a") {
            total = -1;
        }
        else {
            var num = "";
            for (let i = 0; i < current.length; i++) {
                if ($.isNumeric(current.charAt(i))) {
                    num += current.charAt(i);
                }
                else {
                    if (current.charAt(i) == "h") {
                        total += 60 * parseInt(num);
                        num = "";
                    }
                    if (current.charAt(i) == "m") {
                        total += parseInt(num);
                        num = "";
                    }
                }
            }
        }
        options[key] = total;
    }
    return options;
}

processResults({ A: "32 min", B: "1 hour 10 min", C: "2 hours 1 min" })
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda