Created
January 29, 2019 22:09
-
-
Save bogkyu/14b06d16a291194379dbb46244cb65f3 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/bususey
This file contains 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"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* | |
* Keeping Up With the Javascripts | |
* Homework Assignment #6: Loops | |
* | |
* Details: | |
* | |
* You're about to do an assignment called "Fizz Buzz", which is one of the | |
* classic programming challenges. It is a favorite for interviewers, and a | |
* shocking number of job-applicants can't get it right. But you won't be one of | |
* those people. Here are the rules for the assignment (as specified by Imran | |
* Gory): | |
* | |
* Write a program that prints the numbers from 1 to 100. | |
* | |
* But for multiples of three print "Fizz" instead of the number and for the | |
* multiples of five print "Buzz". | |
* | |
* For numbers which are multiples of both three and five print "FizzBuzz". | |
* | |
* ------------- | |
* | |
* Extra Credit: | |
* | |
* Instead of only printing "fizz", "buzz", and "fizzbuzz", add a fourth print | |
* statement: "prime". You should print this whenever you encounter a number that | |
* is prime (divisible only by itself and one). As you implement this, don't worry | |
* about the efficiency of the algorithm you use to check for primes. It's okay | |
* for it to be slow. | |
*/ | |
// creates a prime list under a maximum value | |
'use strict'; | |
function primes(n) { | |
var lst = []; | |
nextCandidate: for (var i = 1; i <= n; ++i) { | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = Array.prototype.slice.call(lst, 1)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
p = _step.value; | |
if (i % p === 0) { | |
continue nextCandidate; | |
} | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator['return']) { | |
_iterator['return'](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
lst.push(i); | |
} | |
return lst; | |
} | |
// console.log(primes(0)); | |
// main function | |
function homeAssign6Loops() { | |
var nmax = arguments.length <= 0 || arguments[0] === undefined ? 100 : arguments[0]; | |
var noprimes = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | |
var primeslst = primes(noprimes ? 0 : nmax); | |
for (var i = 1; i <= nmax; ++i) { | |
var prm = primeslst.indexOf(i) >= 0; | |
var by3 = i % 3 === 0; | |
var by5 = i % 5 === 0; | |
if (prm) console.log('prime');else if (by3 && by5) console.log('FizzBuzz');else if (by3) console.log('Fizz');else if (by5) console.log('Buzz');else console.log(i); | |
} | |
console.log('------------------------------'); | |
} | |
// example calls | |
// as requested from assignment | |
homeAssign6Loops(); | |
// extra | |
homeAssign6Loops(100, false); | |
// lower values | |
homeAssign6Loops(30); | |
homeAssign6Loops(30, false); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* | |
* Keeping Up With the Javascripts | |
* Homework Assignment #6: Loops | |
* | |
* Details: | |
* | |
* You're about to do an assignment called "Fizz Buzz", which is one of the | |
* classic programming challenges. It is a favorite for interviewers, and a | |
* shocking number of job-applicants can't get it right. But you won't be one of | |
* those people. Here are the rules for the assignment (as specified by Imran | |
* Gory): | |
* | |
* Write a program that prints the numbers from 1 to 100. | |
* | |
* But for multiples of three print "Fizz" instead of the number and for the | |
* multiples of five print "Buzz". | |
* | |
* For numbers which are multiples of both three and five print "FizzBuzz". | |
* | |
* ------------- | |
* | |
* Extra Credit: | |
* | |
* Instead of only printing "fizz", "buzz", and "fizzbuzz", add a fourth print | |
* statement: "prime". You should print this whenever you encounter a number that | |
* is prime (divisible only by itself and one). As you implement this, don't worry | |
* about the efficiency of the algorithm you use to check for primes. It's okay | |
* for it to be slow. | |
*/ | |
// creates a prime list under a maximum value | |
function primes(n){ | |
let lst = [] ; | |
nextCandidate: | |
for( let i = 1; i<=n; ++i ){ | |
for( p of Array.prototype.slice.call(lst, 1)) { | |
if (i % p === 0 ) { | |
continue nextCandidate; | |
} | |
} | |
lst.push(i); | |
} | |
return lst; | |
} | |
// console.log(primes(0)); | |
// main function | |
function homeAssign6Loops (nmax = 100, noprimes=true) { | |
let primeslst = primes(noprimes ? 0 : nmax); | |
for( let i= 1; i<=nmax; ++i) { | |
const prm = primeslst.indexOf(i) >= 0 ; | |
const by3 = i % 3 === 0 ; | |
const by5 = i % 5 === 0 ; | |
if( prm ) | |
console.log('prime'); | |
else if( by3 && by5 ) | |
console.log('FizzBuzz'); | |
else if ( by3 ) | |
console.log('Fizz'); | |
else if( by5 ) | |
console.log('Buzz'); | |
else | |
console.log(i); | |
} | |
console.log('------------------------------'); | |
} | |
// example calls | |
// as requested from assignment | |
homeAssign6Loops(); | |
// extra | |
homeAssign6Loops(100, false); | |
// lower values | |
homeAssign6Loops(30); | |
homeAssign6Loops(30, false); | |
</script></body> | |
</html> |
This file contains 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
/* | |
* Keeping Up With the Javascripts | |
* Homework Assignment #6: Loops | |
* | |
* Details: | |
* | |
* You're about to do an assignment called "Fizz Buzz", which is one of the | |
* classic programming challenges. It is a favorite for interviewers, and a | |
* shocking number of job-applicants can't get it right. But you won't be one of | |
* those people. Here are the rules for the assignment (as specified by Imran | |
* Gory): | |
* | |
* Write a program that prints the numbers from 1 to 100. | |
* | |
* But for multiples of three print "Fizz" instead of the number and for the | |
* multiples of five print "Buzz". | |
* | |
* For numbers which are multiples of both three and five print "FizzBuzz". | |
* | |
* ------------- | |
* | |
* Extra Credit: | |
* | |
* Instead of only printing "fizz", "buzz", and "fizzbuzz", add a fourth print | |
* statement: "prime". You should print this whenever you encounter a number that | |
* is prime (divisible only by itself and one). As you implement this, don't worry | |
* about the efficiency of the algorithm you use to check for primes. It's okay | |
* for it to be slow. | |
*/ | |
// creates a prime list under a maximum value | |
'use strict'; | |
function primes(n) { | |
var lst = []; | |
nextCandidate: for (var i = 1; i <= n; ++i) { | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = Array.prototype.slice.call(lst, 1)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
p = _step.value; | |
if (i % p === 0) { | |
continue nextCandidate; | |
} | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator['return']) { | |
_iterator['return'](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
lst.push(i); | |
} | |
return lst; | |
} | |
// console.log(primes(0)); | |
// main function | |
function homeAssign6Loops() { | |
var nmax = arguments.length <= 0 || arguments[0] === undefined ? 100 : arguments[0]; | |
var noprimes = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | |
var primeslst = primes(noprimes ? 0 : nmax); | |
for (var i = 1; i <= nmax; ++i) { | |
var prm = primeslst.indexOf(i) >= 0; | |
var by3 = i % 3 === 0; | |
var by5 = i % 5 === 0; | |
if (prm) console.log('prime');else if (by3 && by5) console.log('FizzBuzz');else if (by3) console.log('Fizz');else if (by5) console.log('Buzz');else console.log(i); | |
} | |
console.log('------------------------------'); | |
} | |
// example calls | |
// as requested from assignment | |
homeAssign6Loops(); | |
// extra | |
homeAssign6Loops(100, false); | |
// lower values | |
homeAssign6Loops(30); | |
homeAssign6Loops(30, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment