Skip to content

Instantly share code, notes, and snippets.

@cachrisman
Created May 9, 2015 01:38
Show Gist options
  • Save cachrisman/02a820636a79cf5a2bba to your computer and use it in GitHub Desktop.
Save cachrisman/02a820636a79cf5a2bba to your computer and use it in GitHub Desktop.
WDI Week 2 problems - Charlie's solutions
// WDI Week 2 problems
// Problem 1: Factorial using recursion
function factorial(n) {
if (n < 0) return "error"; //can't do factorial with negative numbers
else if (n < 2) return 1; //terminal condition
else return factorial(n - 1) * n;
}
console.log("\nfactorial");
console.log(factorial(5)); // => 120
// Problem 2: Determine whether a given string is a palindrome
function isPalindrome(string) {
string = string.replace(/\s/g, ''); // removes all whitespaces(\s) from string
if (string.length === 1 || string.length === 0) return true; // terminal conditions
return (string.charAt(0) === string.charAt(string.length-1) && isPalindrome(string.slice(1,string.length-1)));
}
// solution using for loop
// function isPalindrome(string) {
// for (var i = 0; i < Math.floor(string.length / 2); i++) {
// if (string.charAt(i) !== string.charAt(string.length - 1 - i)) return false;
// }
// return true;
// }
console.log("\nPalindrome");
console.log(isPalindrome('hello')); // => false
console.log(isPalindrome('hannah')); // => true
console.log(isPalindrome('taco cat')); // => true
// Problem 3: Magic Square
squares = [
[[2,7,6],[9,5,1],[4,3,8]],
[[2,7,6],[9,5,1],[4,3,9]], // not magic square
[[4,9,2],[3,5,7],[8,1,6]],
[[7,12,1,14],[2,13,8,11],[16,3,10,5],[9,6,15,4]],
[[37,78,29,70,21,62,13,54,5],[6,38,79,30,71,22,63,14,46],[47,7,39,80,31,72,23,55,15],[16,48,8,40,81,32,64,24,56],[57,17,49,9,41,73,33,65,25],[26,58,18,50,1,42,74,34,66],[67,27,59,10,51,2,43,75,35],[36,68,19,60,11,52,3,44,76],[77,28,69,20,61,12,53,4,45]]
];
//returns the sum of the diagonal from lower right corner to upper left corner
function sumDiagOne(array, col, row) {
col = typeof col == "undefined" ? array.length-1 : col; // if col is not passed to function, set col to array.length-1
row = typeof row == "undefined" ? array.length-1 : row; // if row is not passed to function, set row to array.length-1
if (col === 0 && row === 0) return array[row][col]; // terminal condition
return array[row][col] + sumDiagOne(array, col - 1, row - 1);
}
//returns the sum of the diagonal from upper right corner to lower left corner
function sumDiagTwo(array, col, row) {
col = typeof col == "undefined" ? array.length-1 : col; // if col is not passed to function, set col to array.length-1
row = typeof row == "undefined" ? 0 : row; // if row is not passed to function, set row to 0
if (col === 0 && row === array.length - 1) return array[row][col]; // terminal condition
return array[row][col] + sumDiagTwo(array, col - 1, row + 1);
}
//returns the sum of a row
function sumHoriz(array, row, col) {
col = typeof col == "undefined" ? array.length-1 : col; // if col is not passed to function, set col to array.length-1
if (col === 0) return array[row][col]; // terminal condition
return array[row][col] + sumHoriz(array, row, col - 1);
}
//returns the sum of a column
function sumVert(array, col, row) {
row = typeof row == "undefined" ? array.length-1 : row; // if row is not passed to function, set row to array.length-1
if (row === 0) return array[row][col]; // terminal condition
return array[row][col] + sumVert(array, col, row - 1);
}
//returns true if the sum of both diagonals, each row and each column are equal
function isMagic(array) {
if (sumDiagOne(array) !== sumDiagTwo(array)) {
console.log("diagonals don't match " + sumDiagOne(array) + " " + sumDiagTwo(array));
return false;
}
for (i = 0; i < array.length; i++) {
if (sumHoriz(array, i) !== sumDiagOne(array)) {
console.log("row " + i + " doesn't match");
return false;
}
if (sumVert(array, i) !== sumDiagOne(array)) {
console.log("column " + i + " doesn't match");
return false;
}
}
return true;
}
console.log("\nisMagic");
console.log(isMagic([0])); // => true
console.log(isMagic([1])); // => false
console.log(isMagic([2])); // => true
console.log(isMagic([3])); // => true
console.log(isMagic([4])); // => true
// Problem 4: Determine if an array has any duplicates
// returns true is array has duplicate elements
function hasDupes(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] === arr[j] && i !== j) return true;
}
}
return false;
}
console.log("\nhasDupes");
console.log(hasDupes([1, 2, 3, 4])); // => false
console.log(hasDupes([12, 2, 1, 3, 4, 5, 5])); // => true
// Problem 5: Choose-2
// returns an array of arrays with each combination of 2 elements from the elements of input array
function chooseTwo(arr) {
var output = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (i !== j) output.push([arr[i], arr[j]]);
}
}
return output;
}
console.log("\nchooseTwo");
console.log(chooseTwo([1, 2, 3])); // => [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
console.log(chooseTwo(['a', 'b', 'c', 'd'])); // => [['a','b'],['a','c'],['a','d'],['b','a'],['b','c'],['b','d'],['c','a'],['c','b'],['c','d'],['d','a'],['d','b'],['d','c']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment