Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  185] [ 7]  / answers: 1 / hits: 56177  / 9 Years ago, wed, june 3, 2015, 12:00:00

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>




More From » javascript

 Answers
61



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>




[#66346] Tuesday, June 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeffery

Total Points: 180
Total Questions: 114
Total Answers: 117

Location: Chad
Member since Mon, Dec 5, 2022
2 Years ago
jeffery questions
Fri, Jan 22, 21, 00:00, 3 Years ago
Wed, Dec 2, 20, 00:00, 4 Years ago
Mon, Apr 13, 20, 00:00, 4 Years ago
Mon, Feb 10, 20, 00:00, 4 Years ago
;