简体   繁体   中英

How do I convert line 6 and 7 from python into javascript?

I'm trying to convert this function from python into javascript. but I have no clue how I can convert the for loops from line 6 and 7 into javascript.

async def compute_wn8(stats_totals, exp_stat_totals) -> float:
        """Compute the WN8 of a player."""
        wn8 = 0
        if stats_totals and exp_stat_totals:
            stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
            dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
            exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)

            r_dmg = dmgs / exp_dmgs if exp_dmgs > 0 else 0
            r_spot = spots / exp_spots if exp_spots > 0 else 0
            r_kill = kills / exp_kills if exp_kills > 0 else 0
            r_def = defs / exp_defs if exp_defs > 0 else 0
            r_win = wins / exp_wins if exp_wins > 0 else 0

            r_dmg_c = max(0., (r_dmg - 0.22) / 0.78)
            r_spot_c = max(0., min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62))
            r_kill_c = max(0., min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88))
            r_def_c = max(0., min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90))
            r_win_c = max(0., (r_win - 0.71) / 0.29)

            wn8 += 980 * r_dmg_c
            wn8 += 210 * r_dmg_c * r_kill_c
            wn8 += 155 * r_kill_c * r_spot_c
            wn8 += 75 * r_def_c * r_kill_c
            wn8 += 145 * min(1.8, r_win_c)
        return wn8

This is how far I got:

async function compute_wn8(stats_totals, exp_stat_totals)
        //Compute the WN8 of a player.
        wn8 = 0
        if(stats_totals && exp_stat_totals){
            stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
            dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
            exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)

            if(exp_dmgs>0) {r_dmg = dmgs / exp_dmgs} else r_dmg = 0;
            if(xp_spots>0) {r_spot = spots / exp_spots} else xp_spots = 0;
            if(exp_kills > 0) {r_kill = kills / exp_kills} else exp_kills = 0;
            if(exp_defs > 0) {r_def = defs / exp_defs} else exp_defs = 0;
            if(exp_wins > 0) {r_win = wins / exp_wins} else exp_wins = 0;

            r_dmg_c = max(0., (r_dmg - 0.22) / 0.78)
            r_spot_c = max(0., min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62))
            r_kill_c = max(0., min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88))
            r_def_c = max(0., min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90))
            r_win_c = max(0., (r_win - 0.71) / 0.29)

            wn8 += 980 * r_dmg_c
            wn8 += 210 * r_dmg_c * r_kill_c
            wn8 += 155 * r_kill_c * r_spot_c
            wn8 += 75 * r_def_c * r_kill_c
            wn8 += 145 * min(1.8, r_win_c)
        return wn8
        }   

but I have no clue how to convert these two lines into javascript:

dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)

I hope anyone of you guys can help me :)

Assuming you have stat_totals in an object, you can convert the python comprehension + unpacking into a javascript map + destructure:

 let stats_totals = { dmgs:10, spots:20, kills:30, defs:40, wins:50} let stat_keys = ['dmgs', 'spots', 'kills', 'defs', 'wins'] // dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys) let [dmgs, spots, kills, defs, wins] = stat_keys.map(stat => stats_totals[stat] ) console.log(dmgs, spots, kills, defs, wins) 

The same idea should work for the other line given an object exp_stat_totals .

You may also be able to directly destructure the object:

 let stats_totals = { dmgs:10, spots:20, kills:30, defs:40, wins:50} let {dmgs, spots, kills, defs, wins} = stats_totals console.log(dmgs, spots, kills, defs, wins) 

I've converted code from one language to another in the past. You should avoid translating it literally, but instead trying to understand what the code does and translate that. Differentiating what is logic and what are peculiarities or implementation details of that language.

Although Mark Meyer gave you an answer that translates these lines literally to JS, and it works, I'd recommend not using that code. It's complicated and slow in JS.

Because, unlike Python understanding these comprehensions, JS is not smart enough yet to understand that stat_keys is a static list of property names and to optimize these Aray#map calls followed by the array destructuring.

That's how I'd translate your function:

// Compute the WN8 of a player.
// why is this function async in your code?
function compute_wn8(stats_totals, exp_stat_totals) {
    if (!stats_totals || !exp_stat_totals) {
        return 0;
    }

    const r_dmg    = exp_stat_totals.dmgs > 0 ? stats_totals.dmgs / exp_stat_totals.dmgs : 0;
    const r_spot   = exp_stat_totals.spots > 0 ? stats_totals.spots / exp_stat_totals.spots : 0;
    const r_kill   = exp_stat_totals.kills > 0 ? stats_totals.kills / exp_stat_totals.kills : 0;
    const r_def    = exp_stat_totals.defs > 0 ? stats_totals.defs / exp_stat_totals.defs : 0;
    const r_win    = exp_stat_totals.wins > 0 ? stats_totals.wins / exp_stat_totals.wins : 0;
    const r_dmg_c  = Math.max(0, (r_dmg - 0.22) / 0.78);
    const r_spot_c = Math.max(0, Math.min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62));
    const r_kill_c = Math.max(0, Math.min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88));
    const r_def_c  = Math.max(0, Math.min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90));
    const r_win_c  = Math.max(0, (r_win - 0.71) / 0.29);

    return 980 * r_dmg_c
        + 210 * r_dmg_c * r_kill_c
        + 155 * r_spot_c * r_kill_c
        + 75 * r_def_c * r_kill_c
        + 145 * Math.min(1.8, r_win_c);
}

besides that, even in python, I'm not sure wether this

stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)

r_dmg = dmgs / exp_dmgs if exp_dmgs > 0 else 0
r_spot = spots / exp_spots if exp_spots > 0 else 0
r_kill = kills / exp_kills if exp_kills > 0 else 0
r_def = defs / exp_defs if exp_defs > 0 else 0
r_win = wins / exp_wins if exp_wins > 0 else 0

is any better (shorter, more readable, faster, anything) than this:

r_dmg = stats_totals.dmgs / exp_stat_totals.dmgs if exp_stat_totals.dmgs > 0 else 0
r_spot = stats_totals.spots / exp_stat_totals.spots if exp_stat_totals.spots > 0 else 0
r_kill = stats_totals.kills / exp_stat_totals.kills if exp_stat_totals.kills > 0 else 0
r_def = stats_totals.defs / exp_stat_totals.defs if exp_stat_totals.defs > 0 else 0
r_win = stats_totals.wins / exp_stat_totals.wins if exp_stat_totals.wins > 0 else 0

and please, please declare your variables and use ; in JS. Your future self will thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM