+ 1

Converting a type of data to another type in javascript;

var x="234" I want to concert it into number data type ,

27th Jun 2017, 6:28 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
7 ответов
+ 1
*BEFORE YOU READ - THIS WAS IN RESPONSE TO HIS ACTUAL CODE THAT WASNT POSTED HERE. I SUPPLIED AN ANSWER BELOW TO THE QUESTION ABOVE THOUGH.* You deleted your other thread before I could post. Here is a solution that'll work for you buddy. Hope it helps https://code.sololearn.com/WqzqjXnp2Fj9/#js var s="+234+56*"; var strEnd = s.charAt(s.length-1); var strBeg = s.charAt(0); document.write(s+"<br>"); switch(strBeg){ case "*": case "/": case "-": case "+": document.write("Syntax Error: Invalid Beginning Char ("+ strBeg +")<br>"); break; } switch(strEnd){ case "*": case "/": case "-": case "+": document.write("Syntax Error: Invalid Ending Char ("+ strEnd +")<br>"); break; }
27th Jun 2017, 7:33 PM
AgentSmith
+ 1
what if var c = "23+43+65*" and I want that if last one is operator, the output should be error, I did that, using charAt() and if condition, but problem coming when var c="43+45+76/" in this case my if condition not working
27th Jun 2017, 6:35 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
+ 1
JavaScript has dynamic typing, so in most instances, it'll automatically convert the variable to the correct data type. You can also use something like Number(), parseInt(), or parseFloat(). Number(x); parseInt(x); parseFloat(x); https://www.w3schools.com/js/js_number_methods.asp ^More information if you want it. Hope it helps.
27th Jun 2017, 6:36 PM
AgentSmith
+ 1
thanks, am trying it...
27th Jun 2017, 6:37 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
+ 1
@Netkos Ent, very thanks for ur efforts, keep it up, as ur help can be turn on and inspiration for another...
27th Jun 2017, 7:26 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
+ 1
@Ashish, It's my pleasure. I hope that helped you out some with understanding a couple ways you can go about it. The 'switch' method is much easier on the eyes, and much easier to maintain. I wish you the best on your journey with JS!
27th Jun 2017, 7:31 PM
AgentSmith
0
Sorry, I'm doing my best to understand the question. What type of error do you get with the charAt() stuff? Hard for me to say without seeing your code. Here is example though: var str = "23+43+65/"; var sl = str.length; // grab length of string so we can get last char easily var res = str.charAt(sl-1); // this will look at the last character in your string if(res == "/") { // do this if last char is "/" } else { // otherwise, do this code if it doesn't end with "/" }
27th Jun 2017, 6:46 PM
AgentSmith