Created
January 11, 2012 07:51
-
-
Save djtriptych/1593615 to your computer and use it in GitHub Desktop.
OKCoder examples
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
// fizzbuzz.js | |
// Author: Triptych | |
// Study on fizzbuzz | |
// Note: You can run this code in your scratchpad. Just copy & paste. | |
// | |
// ignore the next line; it's for nerds. | |
if (!console && print) var console = {log:print}; | |
// Correct "fizzbuzz" implementation. | |
// | |
// Loop through numbers from 1-20. If number is evenly divisible by 3 print | |
// 'fizz'; if by 5 print 'buzz', and if by 15 print 'fizzbuzz'. | |
// | |
var fizzbuzz = function () { | |
for (var i=1; i<=20; i++) { | |
if (i % 15 === 0) { // does i divide 15 with no remainder? | |
console.log('fizzbuzz'); // yes, so output 'fizzbuzz' and exit. | |
} | |
else // no, i doesn't divide 15. | |
if (i % 5 === 0) { // does i divide 5 with no remainer? | |
console.log('buzz'); // yes, so output 'buzz' and exit. | |
} | |
else // i divides neither 15 nor 5. | |
if (i % 3 == 0) { // does i divide 3? | |
console.log('fizz'); // yes, so ouput 'fizz' and exit. | |
} | |
else { // i didn't divide anything. | |
console.log(i); // output i itself and exit. | |
} | |
} | |
// exit here. Once a test passes, no more else statements are read and the | |
// code jumps to the end of the function, and nothing else happenes. | |
} | |
// See it in action... | |
fizzbuzz(); |
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
// Incrementing.js | |
// Author: Triptych | |
// A study on the increment (++) operator. | |
// Note: You can run this code in your scratchpad. Just copy & paste. | |
// ignore this; it's so Triptych can run this on a command line. | |
if (!console && print) var console = {log:print}; | |
// Initialize i to 0. | |
var i = 0; | |
// All of these statements are true | |
console.log('Example 1.'); | |
console.log(i === 0); | |
console.log(i++ === 0); | |
console.log(i === 1); | |
// All of these are true too | |
console.log('Example 2.'); | |
console.log(i === 1); | |
console.log(++i === 2); | |
console.log(i === 2); | |
// And these are true too... | |
console.log('Example 3.'); | |
console.log(i === 2); | |
console.log(i++ + ++i === 6); // whoa! | |
console.log(i === 4); | |
/* | |
See what's happening ?? | |
When you use the increment operator (++) AFTER a variable, it increments the | |
variable then returns the OLD value of the variable. This is called postfix | |
form. | |
var i = 2; | |
var j = i++; | |
j===2; | |
i===3; | |
When you use the increment operator BEFORE a variable, it increments it and | |
returns it right then and there. | |
var i = 2; | |
var j = ++i; | |
j===3; | |
i===3; | |
In the very first example, when evaluating (i++ === 0), i was incremented to 1 only | |
after the comparison was made. In the second, i was incremented before the | |
comparison was made. | |
In the crazy-looking (i++ + ++i == 6) example, i is first 2, the the prefix | |
operator increments i to 3, then the addition is performed, with both | |
appearances of i now equal to 3. 3+3===6, so true is returned, then finally the | |
postfix ++ is applied, and i is equal to 4, as we see in the next example. | |
More on the increment operator: | |
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Arithmetic_Operators#.2B.2B_(Increment) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment