Joshua4550 Posted May 4, 2010 Share Posted May 4, 2010 Well here it is, I have a SELECT object, and a TEXT object. I checked them both with a simple alert to see if they worked, and they did.. but he next part didn't. Please help. var answer = parseInt(input.value, base); alert(input.value + " " + answer); input = TEXT object. What this code does is: If i type 5 into the TEXT object, the alert will say that the input.value is 5 but the answer variable still says not a number (NaN) Oh and the base variable was 2 at this time. Please help! Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2010 Share Posted May 5, 2010 I think you are misunderstanding the purpose of the second parameter for parseInt(). The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number If you specify the radix to be 2, then the value to be parsed must be in the format of a binary number, e.g. "1101" (which would be parsed to the decimal value of 13). Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 5, 2010 Author Share Posted May 5, 2010 Thx. How would I do it the way I was trying? Sure I did this before, but forgot how. Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 5, 2010 Author Share Posted May 5, 2010 Nvm, pretty sure (If i remember correctly) I used the toString() method with the radix. Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 5, 2010 Author Share Posted May 5, 2010 function convertToBase() { // Converts depending on selections/inputs var input = document.getElementById('number'); var toBaseObj = document.getElementById('to'); var fromBaseObj = document.getElementById('from'); var index = 0; var toBase = 2; var fromBase = 10; index = fromBaseObj.selectedIndex; fromBase = (index == 0 ? 10 : index == 1 ? 2 : index == 2 ? 16 : index == 3 ? 8 : 10); index = toBaseObj.selectedIndex; toBase = (index == 0 ? 2 : index == 1 ? 10 : index == 2 ? 16 : index == 3 ? 8 : 2); index = 0; var inputValue = parseInt(input.value, fromBase); var answer = inputValue.toString(toBase); alert("Your conversion of " + input.value + " (to base " + base + ") is: \n" + answer); } I don't see errors. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2010 Share Posted May 5, 2010 Why don't you start by explaining what you want to achieve? Your first post was only regarding the NaN error you were receiving. I can "assume" you are trying to convert a base 10 number to a base 2, but that is only a guess since you've never stated that. Your last post you state you see no errors but don't state what you want the script to do and what it is doing differently. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2010 Share Posted May 5, 2010 Try this function convertBase(input, input_base, output_base) { var inputInDec = parseInt(input, input_base); var output = inputInDec.toString(ouput_base); return output; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2010 Share Posted May 5, 2010 Fixed typo: function convertBase(input, input_base, output_base) { var inputInDec = parseInt(input, input_base); var output = inputInDec.toString(output_base); return output; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.