Created
November 10, 2013 21:48
-
-
Save raymondfeng/7404411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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