var primeChecker = {
/**
* Append all successfully found primes to the container
*/
appendResults: function (primes) {
var toAppend = primes.join('* <p></p>'),
resultsContainer = document.getElementById('prime-results');
resultsContainer.innerHTML = toAppend;
},
/**
* Attach event listener for the click action of the "Get primes!" button
*/
attachEventListeners: function () {
var CLICK = 'click',
self = this,
handleEventAction,
inputVal = document.getElementsByTagName('input')[0],
primeCheckBtn = document.getElementById('js-prime-check');
var handleEventAction = function (evt) {
// Need to maintain context to `this` so we'll use call here
self.handleClick.call(self, evt);
};
primeCheckBtn.addEventListener(CLICK, handleEventAction, false);
},
/**
* Checks whether or not the input number is
* @param `num` Number to check if prime
* @return Array of primes
*/
check: function (num) {
var i = 0,
primes = [];
this.attachEventListeners();
for (i; i <= num; i++) {
this.isPrime(i) && primes.push(i);
}
return primes;
},
/**
* Handles the click action of the "Get primes!" button
*/
handleClick: function () {
var inputValue = document.getElementsByTagName('input')[0].value,
results;
if (!isNaN(inputValue)) {
results = this.check(inputValue);
} else {
alert('enter a valid number please');
}
if (results) {
this.appendResults(results);
}
},
/**
* Return true or false if the number passes the prime test
* @param `num` Number from 2 => n where n is the input
* @return Boolean
*/
isPrime: function (num) {
var prime = true,
i =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.