Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this:

C#
for(i=1; i<= num; i++)
    {
     
      loadNotes(resultado[i],resultado[i+num],pos_x,pos_y);
      pos_x = pos_x+200;
      //pos_y = pos_y+250;
      pos_y = 0;
    }
}


The problem is: "i+num" are not being seen as a SUM but as a concat (.).

i.e., lets suppose that num = 5 and for i=1

instead resultado[i+num] = resultado [6] its happening resultado[15].

num and resultado[], are string and array of strings, comes from C#, I really need that way, so I'm wondering how can I solve this problem.

I cant parse the whole array because half of it are really strings
Posted
Updated 23-Dec-10 5:27am
v4

Simply surround the string types with parseInt(num) to cast it to an integer number. You could also use parseFloat. You need to parse all the string types that should actually be a number.

Try this:
loadNotes(parseInt(resultado[i]),parseInt(resultado[parseInt(i)+parseInt(num)])),pos_x,pos_y);


Good luck!
 
Share this answer
 
Comments
Maxdd 7 23-Dec-10 11:33am    
That's what I did :)
Based on your previous questions num is being returned from the server as a string so JavaScript, a loosely typed language, is interrupting the action as a string concatenation and converting i to a string thus producing 15 rather than the 6 you are expecting.

You can either change your server code to return an int or use parseInt in the JavaScript


Also, based on your previous questions I would strongly suggest you pick up a book on JavaScript and read it before continuing.
 
Share this answer
 
Comments
Maxdd 7 23-Dec-10 11:32am    
You are absolutely right!
How about parsing into a new variable?

var mySum = parseInt(i) + parseInt(num)

loadNotes(resultado[i],resultado[mySum],pos_x,pos_y);


Having javascript understand that these are numbers is the only it's going to understand you want to perform a mathmatical operation on them.
 
Share this answer
 
v2
Comments
[no name] 23-Dec-10 16:37pm    
Since i is already an integer why would you need to use parseInt(i)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900