0

I need to use various arrays in a particular function, but I can't help but think I'm doing that in a very inefficient way:

function GetTimeLeft(){
var TimeUnformatted = document.querySelectorAll('[id="SomeIdName"]')[0].innerText.match(/\d{1,}d\s\d{1,}h/ig);
var i;
if (TimeUnformatted){
    var Daysunformatted = [];
    var Hoursunformatted = [];
    var DaysFormatted = [];
    var HoursFormatted = [];
    var DaysToSeconds = [];
    var HoursToSeconds = [];
    var TimeInSeconds = [];
    for (i=0;i<TimeUnformatted.length;i++){
        Daysunformatted[i]  = TimeUnformatted[i].match(/\d{1,}d/)[0];
        Hoursunformatted[i] = TimeUnformatted[i].match(/\d{1,}h/)[0];
        if (Daysunformatted[i])  DaysFormatted[i]  = Number(Daysunformatted[i].match(/\d{1,}/)[0]);
        if (Hoursunformatted[i]) HoursFormatted[i] = Number(Hoursunformatted[i].match(/\d{1,}/)[0]);

        if (DaysFormatted[i])  DaysToSeconds[i]  = DaysFormatted[i]*24*60*60;
        if (HoursFormatted[i]) HoursToSeconds[i] = HoursFormatted[i]*60*60;

        if (DaysToSeconds[i] && HoursToSeconds[i]) TimeInSeconds[i] = DaysToSeconds[i] + HoursToSeconds[i];
    }
    return TimeInSeconds;//an Array.
} else {
    return [0];
}

}

Edit: To make clear, since I expressed myself very poorly. I tried "assigning on the go", without the initial var statements but javascript shouts and tells me he didn't expect the "[":

function GetTimeLeft(){
var TimeUnformatted = document.querySelectorAll('[id="SomeIdName"]')[0].innerText.match(/\d{1,}d\s\d{1,}h/ig);
var i;
if (TimeUnformatted){
    for (i=0;i<TimeUnformatted.length;i++){
        var Daysunformatted[i]  = TimeUnformatted[i].match(/\d{1,}d/)[0];
        var Hoursunformatted[i] = TimeUnformatted[i].match(/\d{1,}h/)[0];
        if (Daysunformatted[i])  var DaysFormatted[i]  = Number(Daysunformatted[i].match(/\d{1,}/)[0]);
        if (Hoursunformatted[i]) var HoursFormatted[i] = Number(Hoursunformatted[i].match(/\d{1,}/)[0]);

        if (DaysFormatted[i])  var DaysToSeconds[i]  = DaysFormatted[i]*24*60*60;
        if (HoursFormatted[i]) var HoursToSeconds[i] = HoursFormatted[i]*60*60;

        if (DaysToSeconds[i] && HoursToSeconds[i]) var TimeInSeconds[i] = DaysToSeconds[i] + HoursToSeconds[i];
    }
    return TimeInSeconds;//an Array.
} else {
    return [0];
}

I know I could do multiple assignments, but still, isn't there a better way to do what I want?

1 Answer 1

1

The problem with that line of code is the var. drop the var and it works just like the code you have above.

Daysunformatted[i]  = TimeUnformatted[i].match(/\d{1,}d/)[0];

only way I could see improving your code is to move the reg exp outside of the for loop.

var reDays = /\d{1,}d/;
var reHours = /\d{1,}h/;
for (i=0;i<TimeUnformatted.length;i++){
        Daysunformatted[i]  = TimeUnformatted[i].match(reDays)[0];
        Hoursunformatted[i] = TimeUnformatted[i].match(reHours)[0];

And you can use capture groups to get the hours so you do not have to do the second match that drops the d/h.

var reDays = /(\d{1,})d/;
var TimeUnformatted = "10d 1h";
 Daysunformatted[i]  = (TimeUnformatted[i].match(reDays) || [,])[1];
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks epascarello! I have two questions, though. 1)Your first advice, assigning without var Daysunformatted[i] = TimeUnformatted[i].match(/\d{1,}d/)[0]; doesn't seem to worrk, for instance Daysunformatted[0] = 3 gives me ReferenceError: Daysunformatted is not defined. 2) Why put the regexp outside the loop? I'm honestly asking that, since I've alrady seen people doing it and it must present some sort of advantage. Finally, really thanks for the "capture groups" advice, it's way more clever than my previous method.
You have to make sure that Daysunformatted is already declared beforehand. AKA var Daysunformatted = [];
Oh, I see what you thought I did, I had explained myself wrong. I edited the post for clarity.
Same problem, you can not do what you are doing, you need to declare the Array first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.