-1
<input id="inp"></input>
<button type="button" onclick="fibbon();">Submit!</button>
<p id="JS"></p>

        <script>
            var n = document.getElementById("inp").value;
            var fibbon = function(n){
                if ((n===1) || (n===2)){
                    return 1;
                }
                else{
                    return fibbon(n-1)+fibbon(n-2);
                }
                document.getElementById("JS").innerHTML = fibbon;
            };

        </script>

I am trying to get this to work with the input, but it doesn't display the number when I put in a number

Emil Dohne
  • 13
  • 1

3 Answers3

2

I think you are looking for something like this...

 <input id="inp"></input>
 <button type="button" onclick="callFibbon();">Submit!</button>
 <p id="JS"></p>

<script>
    function callFibbon(){
       var n = document.getElementById("inp").value;
       if(isNaN(n)){
          alert("Not a valid number");
          return false;
       }
       var val = fibbon(Number(n));
       document.getElementById("JS").innerHTML = val;
    }
    var fibbon = function(n){                
        if ((n===1) || (n===2)){
            return 1;
        }
        else if(n>2){
            return fibbon(n-1)+fibbon(n-2);
        }                
    }
</script>

For inputs:

1 ---result is: 1

2 ---result is: 1

3 ---result is: 2

12----result is: 144

Rajashekhar
  • 469
  • 3
  • 11
-1

You need to convert to number, you might have to invoke the fibbon function passing value as parameter, hope the below function should help :

    <script>
        var n = document.getElementById("inp").value;
        var fibbon = function(n){                
            if ((n===1) || (n===2)){
                return 1;
            }
            else{
                return fibbon(n-1)+fibbon(n-2);
            }                
        };
        document.getElementById("JS").innerHTML = fibbon(+n);
    </script>
Dhananjaya Kuppu
  • 1,322
  • 9
  • 10
-2

Input values are strings by default. You need to convert it into Number.

var fibbon = function(nString){
                    var n = Number(nString);
                    if(n==NaN){
                        alert("Not A Number");
                        return false;
                    }
                    if ((n===1) || (n===2)){
                        return 1;
                    }
                    else{
                        return fibbon(n-1)+fibbon(n-2);
                    }
                    document.getElementById("JS").innerHTML = fibbon;
                };
Tuhin
  • 3,335
  • 2
  • 16
  • 27
  • Recommended practise is to use `isNaN(n)` rather than `n==NaN`, also in the `Html` the value is never passed so `nString` will always be `null`. – Ryan Searle Dec 01 '16 at 11:45
  • Who recommended ?? could you please share?? @RyanSearle – Tuhin Dec 01 '16 at 11:57
  • Have a read http://stackoverflow.com/questions/30314447/how-do-you-test-for-nan-in-javascript. When there is a specific function to do something it's probably save to assume it takes into consideration more situations than otherwise. – Ryan Searle Dec 01 '16 at 12:02
  • I could not see anything saying recommended – Tuhin Dec 01 '16 at 12:05
  • Also that code is nonsense, you do realise that the last line in your function can never be reached? – Ryan Searle Dec 01 '16 at 12:07