Skip to content

Instantly share code, notes, and snippets.

@raymondfeng
Created November 10, 2013 21:48
Show Gist options
  • Select an option

  • Save raymondfeng/7404411 to your computer and use it in GitHub Desktop.

Select an option

Save raymondfeng/7404411 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<input id='num' value=10>
<input id='answer' type='text' readonly>
<button onclick='checkPrime();'>IsPrime</button>
</body>
</html>
function isPrime(x) {
console.log('Is it a prime number? ' + x);
if(x<2||x%2===0) {
return false;
}
for(var i=2; i<x; i++) {
if(x % i === 0) {
console.log('Factor found: ' + i);
return false;
}
}
console.log('Prime: ' +x);
return true;
}
/*
console.log(isPrime(2));
console.log(isPrime(10));
console.log(isPrime(19));
*/
function checkPrime() {
var num = document.getElementById('num');
var x = Number(num.value);
var answer = document.getElementById('answer');
answer.value = isPrime(x)? 'yes': 'no';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment