-4

I am trying to make a factorial calculator. How can I check if the input is empty or not? I tried 'null'. But it didn't work or I couldn't use it properly.
sorry for the stupid question. I am newbie in JavaScript

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function () {
    if( !(isNaN(input))) {
      var result = 1;
      for(var i = 1; i <= input; i++ ) {
        result = result * i
      }
      return result;
    }
    else if (input == null){
      return "Please input a number"
    }
    else{
      return "Please input a number"
    }
  }

  document.getElementById("input2").value = ever();
}
<p>Input: <input type="text" id = "input1" /></p>
<p>Input: <input type="text" id = "input2" /></p> 
<button onclick = "myFriday()">Calculate</button>
<p >RESULT: <span id = "result" style = "color:red"></span> </p>
Naz
  • 2,520
  • 2
  • 16
  • 23

3 Answers3

4

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function() {
    if (input.trim() == '') {
      return "Please input a number"
    } else if (!(isNaN(input))) {
      var result = 1;
      for (var i = 1; i <= input; i++) {
        result = result * i
      }
      return result;
    }

  }

  document.getElementById("input2").value = ever();
}
<p>Input:
  <input type="text" id="input1" />
</p>
<p>Input:
  <input type="text" id="input2" />
</p>
<button onclick="myFriday()">Calculate</button>
<p>RESULT: <span id="result" style="color:red"></span> 
</p>
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • 3
    The question is a duplicate, close it as such. – Ben Fortune Jun 03 '15 at 15:51
  • In my personal opinion, the downvotes may have been cast because this is a trivial question that's likely been asked before, and answering trivial, and likely duplicate questions isn't welcomed by everyone. – SeinopSys Jun 03 '15 at 15:52
  • @BenFortune, I honestly had no idea this is a duplicate and my answer was straight from my head. A downvote should be used for incorrect answers, not answers to a duplicate question – AmmarCSE Jun 03 '15 at 15:53
  • 2
    @AmmarCSE Downvotes represent what people think of your post. As the tooltip says, a downvote means a post is "not useful". You see, if you answer a duplicate question which already has an answer, posting a similar answer may not be considered useful. – SeinopSys Jun 03 '15 at 15:54
  • @DJDavid98 I similar argument was posed on meta some time ago: http://meta.stackoverflow.com/questions/252009/should-there-be-a-deterrent-for-answering-obvious-duplicate-questions#answer-252196. It seems that this may be the _"gray area"_ referred to by the answerer to the post. Is it right to punish the answerer when the poster failed to first search for their question before asking it? Maybe it's time to revisit this issue. Just some food for thought... – War10ck Jun 03 '15 at 15:56
  • @DJDavid98, thanks for your input. To each his own I guess :). What bothers me is, if you think about it, a very **large** set of questions on this site can be considered duplicate. Sigh, oh well – AmmarCSE Jun 03 '15 at 15:56
  • Not only should this question have been closed -- you have no explanation in your answer (as do basically all these answers). As such, in my books, this is a bad answer on a question that should be closed because it has answers elsewhere. – Sterling Archer Jun 03 '15 at 16:47
  • @SterlingArcher, Im sorry but these actions are all biased. I guarantee you that me and you and everybody else have upvoted duplicate answers with no explanations. Like I said before, to each his own. – AmmarCSE Jun 03 '15 at 16:49
  • No, but the lack of explaining why this would work warrants downvotes in many peoples books. – Sterling Archer Jun 03 '15 at 16:50
  • Whatever, you say. The problem is still there. If I click the calculate button with a blank input I get the answer "1". Your conversation didn't helped me at all. – Sajib Devnath Akash Jun 03 '15 at 17:34
  • @SajibDevnathAkash, see the code snippet in my updated answer. It is working now. You need to switch the order of if else if – AmmarCSE Jun 03 '15 at 18:02
  • @AmmarCSE Thanks a lot. That really helped me.Thanks – Sajib Devnath Akash Jun 03 '15 at 18:15
  • In the end I agree with the point of view this should be marked as a duplicate and I should explain my answer. I just don't agree with people forcing me to mark it as duplicate by downvoting. Especially since I answered before anybody even mentioned/gave links indiciating this is duplicate. A simple comment asking me to mark as duplicate would have been much more effective – AmmarCSE Jun 03 '15 at 18:34
0

is that what you looking for?

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function () {
    if(input.match(/\D/) == null){ // changes made here
      var result = 1;
      for(var i = 1; i <= input; i++ ) {
        result = result * i
      }
      return result;
    }
    else{ // one else is enough
      return "Please input a number"
    }
  }

  document.getElementById("input2").value = ever();
}
<p>Input: <input type="text" id = "input1" /></p>
<p>Input: <input type="text" id = "input2" /></p> 
<button onclick = "myFriday()">Calculate</button>
<p >RESULT: <span id = "result" style = "color:red"></span> </p>
Naz
  • 2,520
  • 2
  • 16
  • 23
-3

When you use .value you get a string value in return. This means that when you enter nothing in the input it'll return ""

So you should change this piece of code input == null

Into this input === ""

Note that I also wrote === instead of == Using === in javascript is faster than == when the objects are of the same type.

Jeroen Vervaeke
  • 1,040
  • 9
  • 20