Created
September 10, 2014 03:29
-
-
Save lmullen/1ea04c2c8e1cebd778b6 to your computer and use it in GitHub Desktop.
My solutions to exercises for week 3 of Clio 3
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="beancounting.js"></script> | |
</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
// Count the number of the variable character that occur in string | |
var countChar = function(string, character) { | |
var count = 0; | |
for(var i = 0; i < string.length; i++) { | |
if (string.charAt(i) === character) count++ | |
} | |
return count; | |
} | |
var countBs = function(string) { | |
return countChar(string, "B"); | |
} | |
console.log(countBs("BBC")); | |
console.log(countChar("kakkerlak", "k")); | |
console.log(countChar("mississippi", "i")); | |
console.log(countChar("mississippi", "s")); |
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="chessboard.js"></script> | |
</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
/* | |
* Create a chessboard with a given dimension | |
*/ | |
function chessboard(dim) { | |
// Empty board | |
var board = ""; | |
// This outer loop creates the rows | |
for(var i = 0; i < dim; i++) { | |
// Empty row | |
var row = ""; | |
// This inner loop creates the columns | |
for (var j = 0; j < dim; j++) { | |
if ((i + j) % 2 == 0) | |
row += "#"; | |
else | |
row += " "; | |
} | |
board = board + row + "\n" | |
} | |
return board; | |
} | |
console.log(chessboard(10)); |
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="fizzbuzz.js"></script> | |
</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
for (var i = 1; i <= 100; i++) { | |
if (i % 3 === 0 && i % 5 === 0) console.log("FizzBuzz"); | |
else if (i % 3 === 0) console.log("Fizz"); | |
else if (i % 5 === 0) console.log("Buzz"); | |
else console.log(i); | |
} |
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="minimum.js"></script> | |
</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
// Find the minimum of two numbers | |
var min = function(x, y) { | |
if (x < y) | |
return x; | |
else | |
return y; | |
} | |
console.log(min(0, 10)); | |
console.log(min(0, -10)); |
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="recursive-even.js"></script> | |
</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
// Find out if a number is even to demonstrate recursion | |
function isEven(n) { | |
if (n == 0) | |
return true; | |
else if (n == 1) | |
return false; | |
else if (n < 0) | |
return isEven(-n); | |
else | |
return isEven(n - 2); | |
} | |
console.log("Is 50 even? ", isEven(50)); | |
console.log("Is 75 even? ", isEven(75)); | |
console.log("Is -8 even? ", isEven(-8)); | |
console.log("Is -29 even? ", isEven(-29)); | |
console.log("How long does it take to call this function for 10,000?"); | |
console.time("timer"); | |
console.log("Is 10,000 even?", isEven(10000)); | |
console.timeEnd("timer"); | |
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
</body> | |
<script src="triangle.js"></script> | |
</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
var triangle = ""; | |
for(var i = 0; 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