Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda