JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.11.0/qunit.min.css" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Prime Numbers</title>
</head>
<body>
<div data-role="page">
<code><pre>
/**
* @author Larry Battle - http://bateru.com/news/contact-me
* @date May 11, 2012
* @license MIT and GPL v3
* @purpose Return the prime factors of a number.
* @info - http://bateru.com/news/?s=prime+factors
*/
</pre></code>
<h2>Prime Factors</h2>
<div data-role="fieldcontain">
<label for="userInput">Type in number here:</label>
<input type="text" name="userInput" id="userInput" value="" />
<br/>
<label for="userOutput">Prime Factors:</label>
<input type="text" name="userOutput" id="userOutput" value="" />
</div>
<h1 id="qunit-header">getPrimeFactors Test</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.11.0/qunit.min.js"></script>
<div>
</body>
</html>
JavaScript
/**
* @author Larry Battle - http://bateru.com/news/contact-me
* @date May 11, 2012
* @license MIT and GPL v3
* @purpose Return the prime factors of a number.
* @info - http://bateru.com/news/?s=prime+factors
*/
var getPrimeFactors = function(num) {
num = Math.floor(num);
var root, factors = [], x, sqrt = Math.sqrt, doLoop = 1 < num;
while( doLoop ){
root = sqrt(num);
x = 2;
if (num % x) {
x = 3;
while ((num % x) && ((x += 2) < root));
}
x = (x > root) ? num : x;
factors.push(x);
doLoop = ( x != num );
num /= x;
}
return factors;
}
$(function() {
// Qunit Testing
test("Test invalid input for prime factors", function() {
deepEqual(getPrimeFactors(-10), [] );
deepEqual(getPrimeFactors({}), [] );
deepEqual(getPrimeFactors(0), [] );
deepEqual(getPrimeFactors(1), [] );
deepEqual(getPrimeFactors("ten"), [] );
deepEqual(getPrimeFactors([]), [] );
deepEqual(getPrimeFactors( true ), [] );
});
test("Test prime factors", function() {
deepEqual(getPrimeFactors(2), [2] );
deepEqual(getPrimeFactors(3), [3] );
deepEqual(getPrimeFactors(5), [5] );
deepEqual(getPrimeFactors(4), [2,2] );
deepEqual(getPrimeFactors(10), [2,5] );
deepEqual(getPrimeFactors(1034), [2,11,47] );
deepEqual(getPrimeFactors(5467154436746477), [5467154436746477] );
deepEqual(getPrimeFactors(5467154436746479), [31,31,5689026469039] );
deepEqual(getPrimeFactors(123456789), [3,3,3607,3803] );
deepEqual(getPrimeFactors(987654321), [3,3,17,17,379721] );
deepEqual(getPrimeFactors(300000000000 ), [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5] );
});
// User Controls
$( "#userInput" ).keypress(function(){
var result = getPrimeFactors( $(this).val() );
if( !result.length){
...