user4509200
user4509200

Reputation:

The code not working. I cannot identify the issue

I was writing some code and for some reason that I am unaware of, whenever I click on the button, the function that it is assigned does not run. Does anybody know why? Thanks in advance.

<html>
<head>
</head>
<body>


<script>

function prime(number) {
var text = document.getElementById("p").innerHTML;
var n = 0;

for(var i = 2; i<number; i++){
if(number%i==0){
text = "Your number, "+number+", is divisible by "+i+"! It's composite!";
n = 1;
}
}

if(n==0){
text = "Your number, "+number+", is prime!";
}

}

function funcito(){
console.log("Functions!");
var number = document.getElementById("input");
prime(number);    
}

</script>


<p id="p"></p><br>

<form id="input">
Your Number: <input type="number" name="uInput"><br>
</form> 

<p>Click "Got my number" to find out!.</p>
<button onclick="funcito()" value = "Got my number">Got my number</button>

</body>
</html>

Upvotes: 0

Views: 32

Answers (3)

MLDev
MLDev

Reputation: 1277

There are a couple of other issues with that code, so you won't get the result you are looking for. Try this code:

<html>
<head>
</head>
<body>


<script>

function prime(number) {
var text = document.getElementById("p");
var n = 0;

for(var i = 2; i<number; i++){
if(number%i==0){
text.innerHTML = "Your number, "+ number+", is divisible by "+i+"! It's composite!";
n = 1;
}
}

if(n==0){
text.innerHTML = "Your number" + number + " is prime!";
}

}

function funcito(){
console.log("Functions!");
var number = document.getElementById("input").value;
prime(number);    
}

</script>


<p id="p"></p><br>

<form>
Your Number: <input id = "input" type="number" name="uInput"><br>
</form> 

<p>Click "Got my number" to find out!.</p>
<button  onclick="funcito()" value = "Got my number">Got my number</button>

</body>
</html>

Upvotes: 0

ketan
ketan

Reputation: 19341

Because you write onclick="funcito" which is wrong write onclick="funcito()". instead.

Upvotes: 0

Albert Tai
Albert Tai

Reputation: 141

Try this

<button onclick="funcito()" value = "Got my number">Got my number</button>

Upvotes: 1

Related Questions