Last active
August 29, 2015 14:22
-
-
Save cs3b/5eacdd4cdf57f720edac to your computer and use it in GitHub Desktop.
Eloquent JavaScript (chapter 2 # Excersise)
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
| chessBoard = (size, chars) -> | |
| result = '' | |
| for i in [1..size*size] | |
| result += chars[i % chars.length] | |
| if i % size is 0 | |
| result += '\n' | |
| if size % chars.length is 0 | |
| chars = [chars[1], chars[0]] | |
| i++ | |
| result | |
| console.log chessBoard(9, ['#', ' ']) |
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
| function chessBoard(size, chars) { | |
| var result = ""; | |
| for (var i = 1; i <= size*size; i++) { | |
| result += chars[i % chars.length]; | |
| if((i % size) == 0) { | |
| result += "\n"; | |
| if((size % chars.length) == 0) { | |
| chars = [chars[1], chars[0]]; | |
| } | |
| } | |
| } | |
| return(result); | |
| } | |
| console.log(chessBoard(8, ["#", " "])); |
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
| fizzBuzz = (i, fizz_divider, buzz_divider) -> | |
| if i % fizz_divider is 0 and i % buzz_divider is 0 | |
| 'FizzBuzz' | |
| else if i % fizz_divider is 0 | |
| 'Fizz' | |
| else if i % buzz_divider is 0 | |
| 'Buzz' | |
| else | |
| i | |
| for i in [1..100] | |
| console.log fizzBuzz(i, 2, 4) | |
| i++ |
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
| function fizzBuzz(i, fizz_divider, buzz_divider) { | |
| if ((i % fizz_divider) == 0 && (i % buzz_divider) == 0) { | |
| return("FizzBuzz"); | |
| } else if ((i % fizz_divider) == 0) { | |
| return("Fizz"); | |
| } else if ((i % buzz_divider) ==0) { | |
| return("Buzz"); | |
| } else { | |
| return(i); | |
| } | |
| } | |
| for (var i = 1; i <= 100; i++) { | |
| console.log(fizzBuzz(i, 2, 4)); | |
| } |
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
| var triangle = ''; | |
| for (var i = 1; i<=7; i++) { | |
| triangle += '#'; | |
| console.log(triangle); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment