Created
January 23, 2015 09:05
-
-
Save alex-dixon/7aaf68541f24ea86a494 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/caney
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Chapter 3 Exercises | |
// Minimum | |
function min(num1, num2) { | |
if (num1 === num2) | |
return undefined; | |
else if (num1 < num2) | |
return num1; | |
else | |
return num2; | |
} | |
console.log(min(0, 10)); | |
// 0 | |
console.log(min(0, -10)); | |
// -10 | |
// Recursion | |
function isEven(num) { | |
if (num < 0) | |
num = num * -1; | |
if (num === 1) | |
return false; | |
else if (num === 0) | |
return true; | |
return isEven(num - 2); | |
} | |
console.log(isEven(-2)); | |
// Bean Counting | |
function countBs(str) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === "B") | |
count++; | |
return count; | |
} | |
function countChar(str, char) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === char) | |
count++; | |
return count; | |
} | |
console.log(countBs("BBC")); | |
console.log(countChar("kakkerlak", "k")); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Chapter 3 Exercises | |
// Minimum | |
function min(num1, num2) { | |
if (num1 === num2) | |
return undefined; | |
else if (num1 < num2) | |
return num1; | |
else | |
return num2; | |
} | |
console.log(min(0, 10)); | |
// 0 | |
console.log(min(0, -10)); | |
// -10 | |
// Recursion | |
function isEven(num) { | |
if (num < 0) | |
num = num * -1; | |
if (num === 1) | |
return false; | |
else if (num === 0) | |
return true; | |
return isEven(num - 2); | |
} | |
console.log(isEven(-2)); | |
// Bean Counting | |
function countBs(str) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === "B") | |
count++; | |
return count; | |
} | |
function countChar(str, char) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === char) | |
count++; | |
return count; | |
} | |
console.log(countBs("BBC")); | |
console.log(countChar("kakkerlak", "k")); | |
</script></body> | |
</html> |
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
// Chapter 3 Exercises | |
// Minimum | |
function min(num1, num2) { | |
if (num1 === num2) | |
return undefined; | |
else if (num1 < num2) | |
return num1; | |
else | |
return num2; | |
} | |
console.log(min(0, 10)); | |
// 0 | |
console.log(min(0, -10)); | |
// -10 | |
// Recursion | |
function isEven(num) { | |
if (num < 0) | |
num = num * -1; | |
if (num === 1) | |
return false; | |
else if (num === 0) | |
return true; | |
return isEven(num - 2); | |
} | |
console.log(isEven(-2)); | |
// Bean Counting | |
function countBs(str) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === "B") | |
count++; | |
return count; | |
} | |
function countChar(str, char) { | |
var count = 0; | |
for (i = 0; i < str.length; i++) | |
if (str.charAt(i) === char) | |
count++; | |
return count; | |
} | |
console.log(countBs("BBC")); | |
console.log(countChar("kakkerlak", "k")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment