0
var Height=  (rowData.length * 30) + PPPP.top + 10 ;

When i print this i get 9013510... instead of 90 +135+10 = 235. Why does mine turns into concatentaion instead of Addition.

John Cooper
  • 7,343
  • 31
  • 80
  • 100

4 Answers4

3

You probably need to convert PPPP.top to a number, eg.

var Height = (rowData.length * 30) + parseFloat(PPPP.top) + 10;
mqchen
  • 4,195
  • 1
  • 22
  • 21
2

PPPP.top is probably a string. Try:

var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10 ;
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • +1 for specifying the radix when calling [parseInt](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt). – Anders Fjeldstad Jul 07 '11 at 10:29
1

It's probably treating one of the values incorrectly as a string. Try using parseInt and see if that works:

var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10;
duncan
  • 31,401
  • 13
  • 78
  • 99
  • haha, yes, maybe overkill! In fact as everyone else spotted it's really just the PPPP.top value that requires parseInt – duncan Jul 07 '11 at 11:39
1

You can use parseInt for that.

var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10 ;

I have changed radix to base 10.

Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
  • 2
    parseInt should always be given the radix parameter if you want to avoid something like a user's input of 08 being turned into 0 – duncan Jul 07 '11 at 10:30
  • Yes, Thanks. But you know when validation will come into play we have to make sure that everything is ok. Here I just gave the hint how can you handle it. There could be many ways how you want your user input the value. – Talha Ahmed Khan Jul 07 '11 at 10:34