1

I'm not able to convert integer of ranged string. I have a age group of 21-30, 31-40,41-50 etc, I have to convert selected string and send to server. I have tried with parseInt method but not working. Below is the code,

var ageSelected = '31-40';
parseInt(ageSelected) //Output is only first number 31 NOT 31-40

Please anybody have idea about this, please help me

5
  • - is not an int use parseInt(ageSelected.split('-')[0]) for first number parseInt(ageSelected.split('-')[1]) for second number Commented Jan 29, 2018 at 6:18
  • @guradio Ok then how do I convert for ranging numbers like 1-10, 11-20 etc.. is there any other way to convert Commented Jan 29, 2018 at 6:20
  • 2
    what should be the output from 31-40 input, since 31-40 is not a valid integer? Commented Jan 29, 2018 at 6:20
  • see comment above..split them into 2 numbers then convert Commented Jan 29, 2018 at 6:20
  • you cant send 31-40 as an integer because it is not one. If you just have to send this variable as 31-40 is has to be the string only. Commented Jan 29, 2018 at 6:28

1 Answer 1

2

System don't understand what is 31-40. It can't be a valid integer. You can either send it as a String to the server and process there or send as two separate integers.

var ageSelected = '31-40';
parseInt(ageSelected.split("-")[0]) //start age
parseInt(ageSelected.split("-")[1]) // end age
Sign up to request clarification or add additional context in comments.

4 Comments

@Suresh, Can't we append - by two integers and send to server
That will again become a String that you have processed.
@MallikarjunHampannavar why you cant send that as a string?
@Sures.. Ok thank u, it will become again string only.. I will talk with web team, will try to send as a string.. Thank you Suresh

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.