-1

The code below is returning gracz.N0P but I want it to return 1, 3, 0 etc. I don't want to use if() and switch().

What am I doing wrong?

var gracz = {
    N0P: 1,
    N1P: 3,
    N2P: 0,
    (...)
}
function showType(nr)
{
    return "gracz.N" + nr + "P";
}
Eryczal
  • 23
  • 2

1 Answers1

0

Instead of returning a string, return the object property. Try this:

function showType(nr){
    return gracz['N' + nr + 'P'];
}

You can easily access properties dynamically by using the “bracket notation”.

dakab
  • 5,379
  • 9
  • 43
  • 67