1

In Javascript, I'm trying to make a function which will find the longest number in a string and convert it to a floating point value. However, when I input something like ff333sds22442, it will output "333" as the longest number when it should output 22442.

<input type="text" id="numberInput"></input>
<button onclick="findNumber()"> Find Number </button>
<button onclick="makeSubstring()"> Make Substring </button>
<p id="number"></p>

<script>

function findNumber() {
     var input = document.getElementById("numberInput").value;
     var longest = "";
     var result = "";
     var inputSub = "";

     for (var i = 0; i < input.length; i++) {
         for (var j = i + 1; j < input.length; j++) {

             inputSub = input.substring(i, j + 1);
             if (!isNaN(parseFloat(inputSub)) && inputSub.length > longest.length){
                longest = inputSub;
                result = inputSub;
             }
         }
     }

     result = parseFloat(result);
     document.getElementById("number").innerHTML = result;
}

I'm not sure why it's doing this, I was hoping someone could explain it to me.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • Aside from being not very efficient to start with, when you compare `inputSub.length` you are comparing the length of the substring, not the length of the *parsed* float. So result will always end up being the original string (because it's the longest). And `parseFloat` on that will give you the first number it finds (namely `333`) – Matt Burland Nov 07 '14 at 02:21
  • Is there a more efficient way to sort through this that I should use? – Steven Tchadenis Nov 07 '14 at 02:29

1 Answers1

0

The reason this doesn't work properly is that you are testing to see if parseFloat(inputSub) is NaN. This doesn't work because parseFloat('333sds') will return 333, not NaN.

The documentation for parseFloat contains a stricter parse function that you could use to fix the bug in your function:

var filterFloat = function (value) {
    if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
      .test(value))
      return Number(value);
  return NaN;
}

console.log(filterFloat('421'));               // 421
console.log(filterFloat('-421'));              // -421
console.log(filterFloat('+421'));              // 421
console.log(filterFloat('Infinity'));          // Infinity
console.log(filterFloat('1.61803398875'));     // 1.61803398875
console.log(filterFloat('421e+0'));            // NaN
console.log(filterFloat('421hop'));            // NaN
console.log(filterFloat('hop1.61803398875'));  // NaN
devuxer
  • 41,681
  • 47
  • 180
  • 292
  • No problem. I just added an edit to my answer with something that may help you solve the problem, and it introduces you to regular expressions, if you haven't encountered those yet. – devuxer Nov 07 '14 at 02:43
  • I haven't encountered regular expressions yet. I hope you aren't talking about the if statement in there, because that looks nasty and I don't want to encounter that again anytime soon. – Steven Tchadenis Nov 07 '14 at 02:48
  • Welcome to regular expressions :) They are not easy to read, but they are very powerful, and once you get the hang of them, you will find a lot of uses for them. Try out a tutorial (e.g., http://javascript.info/tutorial/regular-expressions-javascript), and it won't seem so bad. – devuxer Nov 07 '14 at 02:50
  • Thanks a bunch. I'm new here and it's really reassuring that someone is willing to reach out and help me so much! – Steven Tchadenis Nov 07 '14 at 02:54
  • No problem. If you want to pay me back, click the checkbox next to my answer. That let's everyone know that my answer was what you were looking for. If you're feeling particularly generous, you can also click the up arrow (that will give me 10 extra points :)). – devuxer Nov 07 '14 at 02:56
  • Apparently I can't vote up yet, I need more reputation for that. :/ – Steven Tchadenis Nov 07 '14 at 03:58