Home > database >  Javascript function not returning dictionary which it should be
Javascript function not returning dictionary which it should be

Time:04-08

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!

CodePudding user response:

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

CodePudding user response:

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>

  • Related