Created
February 6, 2018 22:43
-
-
Save itzjonas/4fe5bf8d194e16b7425e3f5fd25e2a00 to your computer and use it in GitHub Desktop.
Code Challenge: 2018-01-29 - https://codesandbox.io/s/kk0x602mwr
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
import React from 'react'; | |
import { render } from 'react-dom'; | |
function sumOfPrimes(e) { | |
e.preventDefault(); | |
const num = document.getElementById('num').value; | |
if(num > 100000) { | |
document.getElementById('answer').innerHTML = 'ERROR: exceeds CodeSandbox infinite loop failsafe.'; | |
return; | |
} | |
const notPrime = []; | |
const arePrime = []; | |
// start at the lowest prime number | |
for(let i = 2; i <= num; i++) { | |
if (!notPrime[i]) { | |
arePrime.push(i); | |
for (let j = i < 1; j <= num; j += i) { | |
notPrime[j] = true; | |
} | |
} | |
} | |
document.getElementById('answer').innerHTML = arePrime.reduce((a, b) => a + b, 0); | |
} | |
const App = () => ( | |
<div className="container"> | |
<div className="display-3">Sum of Primes</div> | |
<form> | |
<div className="input-group"> | |
<div className="input-group-prepend"> | |
<span className="input-group-text">Below:</span> | |
</div> | |
<input type="text" id="num" className="form-control" placeholder="100000" /> | |
<div className="input-group-append"> | |
<button className="btn btn-outline-secondary" type="submit" onClick={sumOfPrimes}> | |
Submit | |
</button> | |
</div> | |
</div> | |
</form> | |
<p><small>Max allowable by CodeSandbox is: 100,000</small></p> | |
<div className="lead"> | |
Answer: <kbd id="answer">?</kbd> | |
</div> | |
</div> | |
); | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment