Skip to main content
Known Participant
December 15, 2020
Answered

Error message "NaN" with a factorial calculator

  • December 15, 2020
  • 1 reply
  • 534 views

I'm trying to create a factorial calculator in Dreamweaver using HTML and Javascript.

I'm encountering an error when I preview the code and try to use the calculator, the error message "NaN" pops up instead of a number. I've been told the error is somwhere in this line of  code;

document.getElementById('answer').innerHTML = "Your Answer Is" * a;

but I can't figure out whats wrong with it. I'm quite inexperienced with Javascript and coding so a dumbed down answer would be appreciated.

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<meta lang="en">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	
	<title>Untitled Document</title>
	<link rel="stylesheet" href="factoral.css">
</head>

<body>
	<h3>Enter Your Number</h3>
	<input type="text" id="que">
	
	<input type="button" value="click me" onclick="myFunction()">
	
	<br><br>
	<p id="answer"></p>
	

</body>
<script>
	function myFunction(){
		var x = document.getElementById('que').value;
		var a = 1;
		for (i =1; i <= x; i++){
			a  *= i;
			console.log(a)
		}
		document.getElementById('answer').innerHTML = "Your Answer Is" * a;
	}
</script>
</html>

 

    This topic has been closed for replies.
    Correct answer BenPleysier

    Try:

    "Your Answer Is" + a

    1 reply

    BenPleysier
    Community Expert
    BenPleysierCommunity ExpertCorrect answer
    Community Expert
    December 15, 2020

    Try:

    "Your Answer Is" + a
    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Known Participant
    December 15, 2020

    Thanks again!

    Legend
    December 15, 2020
    document.getElementById('answer').innerHTML = "Your Answer Is" * a;

     

    Using `backtick` template literals is an easier way these days:

     

    document.getElementById('answer').innerHTML = `Your answer Is ${a}`;

     

    Its a cleaner workflow than using the old "Something" + variable + "SomethingElse" method, especially in complex situations. Both are perfectly ok but times move on. I know its made my job easier when working with javascript.