From dd0c2c4ee93fa8cfec15155ea2bcfb6ee2ad9f0d Mon Sep 17 00:00:00 2001 From: Logan Tegman Date: Tue, 6 Oct 2015 23:20:53 -0700 Subject: [PATCH 1/6] Fix BF: Everything Be True Description and Tests - Switch back to being about looking for truth properties - Tests looking for a full key:value pair instead of just a key were removed to clarify the goal. - Added more tests - Removed misleading MDN links Closes #3394 and #2684 --- challenges/intermediate-bonfires.json | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/challenges/intermediate-bonfires.json b/challenges/intermediate-bonfires.json index cc15964181..8dd94eab4a 100644 --- a/challenges/intermediate-bonfires.json +++ b/challenges/intermediate-bonfires.json @@ -761,14 +761,13 @@ "id": "a10d2431ad0c6a099a4b8b52", "title": "Everything Be True", "description": [ - "Check if the predicate (second argument) returns truthy (defined) for all elements of a collection (first argument).", - "For this, check to see if the property defined in the second argument is present on every element of the collection.", + "Check if the predicate (second argument) is truthy on all elements of a collection (first argument).", "Remember, you can access object properties through either dot notation or [] notation.", "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." ], "challengeSeed": [ "function every(collection, pre) {", - " // Does everyone have one of these?", + " // Is everyone being true?", " return pre;", "}", "", @@ -776,12 +775,14 @@ ], "tests": [ "assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\"), true, 'message: every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\") should return true.');", - "assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"}), false, 'message: every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"}) should return false.');", - "assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"female\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"}), false, 'message: every([{\"user\": \"Tinky-Winky\", \"sex\": \"female\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"}) should return false.');" - ], - "MDNlinks": [ - "Object.hasOwnProperty()", - "Object.getOwnPropertyNames()" + "assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\"), false, 'message: every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\") should return false.');", + "assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\", \"age\": 2}, {\"user\": \"Dipsy\", \"sex\": \"male\", \"age\": 0}, {\"user\": \"Laa-Laa\", \"sex\": \"female\", \"age\": 5}, {\"user\": \"Po\", \"sex\": \"female\", \"age\": 4}], \"age\"), false, 'message: every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\", \"age\": 0}, {\"user\": \"Dipsy\", \"sex\": \"male\", \"age\": 3}, {\"user\": \"Laa-Laa\", \"sex\": \"female\", \"age\": 5}, {\"user\": \"Po\", \"sex\": \"female\", \"age\": 4}], \"age\") should return false.');", + "assert.strictEqual(every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true}, {\"name\": \"FastFoward\", \"onBoat\": null}], \"onBoat\"), false, 'message: every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true}, {\"name\": \"FastFoward\", \"onBoat\": null}], \"onBoat\") should return false');", + "assert.strictEqual(every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true, \"alias\": \"Repete\"}, {\"name\": \"FastFoward\", \"onBoat\": true}], \"onBoat\"), true, 'message: every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true, \"alias\": \"Repete\"}, {\"name\": \"FastFoward\", \"onBoat\": true}], \"onBoat\") should return true');", + "assert.strictEqual(every([{\"single\": \"yes\"}], \"single\"), true, 'message: every([{\"single\": \"yes\"}], \"single\") should return true');", + "assert.strictEqual(every([{\"single\": \"\"}, {\"single\": \"double\"}], \"single\"), false, 'message: every([{\"single\": \"\"}, {\"single\": \"double\"}], \"single\") should return false');", + "assert.strictEqual(every([{\"single\": \"double\"}, {\"single\": undefined}], \"single\"), false, 'message: every([{\"single\": \"double\"}, {\"single\": undefined}], \"single\") should return false');", + "assert.strictEqual(every([{\"single\": \"double\"}, {\"single\": NaN}], \"single\"), false, 'message: every([{\"single\": \"double\"}, {\"single\": NaN}], \"single\") should return false');" ], "type": "bonfire", "challengeType": 5, From 120c3147c2cf8886524241bb1687b181871f0b96 Mon Sep 17 00:00:00 2001 From: Logan Tegman Date: Sat, 17 Oct 2015 23:04:37 -0700 Subject: [PATCH 2/6] Add Better Explanations to the Functional Programming Lessons Other minor changes to make each lesson more consistent in formatting. --- ...t-oriented-and-functional-programming.json | 99 +++++++++++-------- 1 file changed, 58 insertions(+), 41 deletions(-) diff --git a/challenges/object-oriented-and-functional-programming.json b/challenges/object-oriented-and-functional-programming.json index d80a50de85..10defbc1bb 100644 --- a/challenges/object-oriented-and-functional-programming.json +++ b/challenges/object-oriented-and-functional-programming.json @@ -178,17 +178,19 @@ "title":"Iterate over Arrays with .map", "difficulty":0, "description":[ - "The map method is one of the easiest ways to iterate through an array or object there is. Let's use it now.", - "array = array.map(function(val){", - "  return val+1;", + "The map method is a convenient way to iterate through arrays. Here's an example usage:", + "var timesFour = array.map(function(val){", + "  return val*4;", "});", "", + "The map method will iterate through every element of the array, creating a new array with values that have been modified by the callback function, and return it.", + "In our example the callback only uses the value of the array element (the val argument) but your callback can also include arguments for the index and array being acted on.", "Use the map function to add 3 to every value in the variable array." ], "tests":[ "assert.deepEqual(array, [4,5,6,7,8], 'message: You should add three to each value in the array.');", - "assert(editor.getValue().match(/\\.map\\s*\\(/gi), 'message: You should be making use of the map method.');", - "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\]/gi), 'message: You should only modify the array with .map.');" + "assert(editor.getValue().match(/\\.map\\s*\\(/gi), 'message: You should be making use of the map method.');", + "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\]/gi), 'message: You should only modify the array with map.');" ], "challengeSeed":[ "//Use map to add three to each value in the array", @@ -211,15 +213,18 @@ "title":"Condense arrays with .reduce", "difficulty":0, "description":[ - "Reduce can be useful for condensing an array of numbers into one value.", + "The array method reduce is used to iterate through an array and condense it into one value.", + "To use reduce you pass in a callback whose arguments are an accumulator (in this case, previousVal) and the current value (currentVal).", + "reduce has an optional second argument which can be used to set the initial value of the accumulator. If no initial value is specified if will be the first array element.", + "Here is an example of reduce being used to sum all the values of an array:", "var singleVal = array.reduce(function(previousVal, currentVal){", "  return previousVal+currentVal;", "});", - "Use the reduce function to sum all the values in array and assign it to singleVal." + "Use the reduce method to sum all the values in array and assign it to singleVal." ], "tests":[ "assert(singleVal == 30, 'message: singleVal should be equal to the sum of all items in the array variable.');", - "assert(editor.getValue().match(/\\.reduce\\s*\\(/gi), 'message: You should have made use of the reduce method.');" + "assert(editor.getValue().match(/\\.reduce\\s*\\(/gi), 'message: You should have made use of the reduce method.');" ], "challengeSeed":[ "var array = [4,5,6,7,8];", @@ -242,24 +247,27 @@ "title":"Filter Arrays with .filter", "difficulty":0, "description":[ - "Filter is a useful method that can filter out values that don't match a certain criteria", - "Let's remove all the values greater than five", + "The filter method is used to iterate through an array and filter out elements where a given condition is not true.", + "filter is passed a callback function which takes the current value (we've called that val) as an argument. It can also use arguments for the index and array being acted on.", + "Any array element for which the callback returns true will be kept and elements that return false will be filtered out.", + "The following code is an example of using filter to remove array elements that are not even numbers:", "array = array.filter(function(val) {", - "  return val <= 5;", - "});" + "  return val % 2 === 0;", + "});", + "Use filter to remove all elements from array that are greater than 5." ], "tests":[ "assert.deepEqual(array, [1,2,3,4,5], 'message: You should have removed all the values from the array that are greater than 5.');", - "assert(editor.getValue().match(/array\\.filter\\s*\\(/gi), 'message: You should be using the filter method to remove the values from the array.');", - "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\,6\\,7\\,8\\,9\\,10\\]/gi), 'message: You should only be using .filter to modify the contents of the array.');" + "assert(editor.getValue().match(/array\\.filter\\s*\\(/gi), 'message: You should be using the filter method to remove the values from the array.');", + "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\,6\\,7\\,8\\,9\\,10\\]/gi), 'message: You should only be using filter to modify the contents of the array.');" ], "challengeSeed":[ "var array = [1,2,3,4,5,6,7,8,9,10];", - " // Only change code below this line.", + "// Only change code below this line.", "", "", "", - " // Only change code above this line.", + "// Only change code above this line.", "(function() {return array;})();" ], "MDNlinks":[ @@ -273,16 +281,20 @@ "title": "Sort Arrays with .sort", "difficulty":0, "description":[ - "You can use the method sort to easily sort the values in the array alphabetically or numerically.", - "var array = [1, 3, 2];", - "array = array.sort();", - "array is now [1, 2, 3].", + "You can use the method sort to easily sort the values in an array alphabetically or numerically.", + "Unlike the previous array methods we have been looking at, sort actually alters the array in place. However, it also returns this sorted array.", + "sort can be passed a compare function as a callback. If no compare function is passed in it will convert the values to strings and sort alphabetically.", + "Here is an example of using sort with a compare function that will sort the elements from smallest to largest number:", + "var array = [1, 12, 21, 2];", + "array.sort(function(a, b) {", + "  return a - b;", + "});", "Use sort to sort array alphabetically." ], "tests":[ "assert.deepEqual(array, ['alpha', 'beta', 'charlie'], 'message: You should have sorted the array alphabetically.');", - "assert(editor.getValue().match(/\\[\\'beta\\'\\,\\s\\'alpha\\'\\,\\s'charlie\\'\\];/gi), 'message: You should only be using .sort to modify the array.');", - "assert(editor.getValue().match(/\\.sort\\s*\\(\\)/gi), 'message: You should have made use of the sort method.');" + "assert(editor.getValue().match(/\\[\\'beta\\'\\,\\s\\'alpha\\'\\,\\s'charlie\\'\\];/gi), 'message: You should only be using sort to modify the array.');", + "assert(editor.getValue().match(/\\.sort\\s*\\(\\)/gi), 'message: You should have made use of the sort method.');" ], "challengeSeed":[ "var array = ['beta', 'alpha', 'charlie'];", @@ -290,7 +302,7 @@ "", "", "", - " // Only change code above this line.", + "// Only change code above this line.", "(function() {return array;})();" ], "MDNlinks":[ @@ -303,7 +315,8 @@ "id": "cf1111c1c16feddfaeb2bdef", "title": "Reverse Arrays with .reverse", "description": [ - "You can use the reverse function to reverse the contents of an array.", + "You can use the reverse method to reverse the elements of an array.", + "reverse is another array method that alters the array in place, but it also returns the reversed array.", "Add a line of code that uses reverse to reverse the array variable." ], "tests": [ @@ -313,11 +326,11 @@ ], "challengeSeed": [ "var array = [1,2,3,4,5,6,7];", - " // Only change code below this line.", + "// Only change code below this line.", "", "", "", - " // Only change code above this line.", + "// Only change code above this line.", "(function() {return array;})();" ], "MDNlinks":[ @@ -330,12 +343,14 @@ "id": "cf1111c1c16feddfaeb3bdef", "title": "Concatenate Strings with .concat", "description": [ - ".concat() can be used to merge the contents of two arrays into one.", + "concat can be used to merge the contents of two arrays into one.", + "concat takes an array as an argument and returns a new array with the elements of this array concatenated onto the end.", + "Here is an example of concat being used to concatenate otherArray onto the end of array:", "array = array.concat(otherArray);", - "Use .concat() to concatenate concatMe onto the end of array and assign it back to array." + "Use .concat() to concatenate concatMe onto the end of array and assign it back to array." ], "tests": [ - "assert.deepEqual(array, [1,2,3,4,5,6], 'message: You should concat the two arrays together.');", + "assert.deepEqual(array, [1,2,3,4,5,6], 'message: You should concatenate the two arrays together.');", "assert(editor.getValue().match(/\\.concat\\s*\\(/gi), 'message: You should be using the concat method to merge the two arrays.');", "assert(editor.getValue().match(/\\[1\\,2\\,3\\]/gi) && editor.getValue().match(/\\[4\\,5\\,6\\]/gi), 'message: You should only be using concat to modify the arrays.');" ], @@ -361,14 +376,15 @@ "title":"Split Strings with .split", "difficulty":0, "description":[ - "You can use the .split() method to split a string into an array.", - ".split() uses the argument you pass in as a delimiter to determine which points the string should be split at.", + "You can use the split method to split a string into an array.", + "split uses the argument you pass in as a delimiter to determine which points the string should be split at.", + "Here is an example of split being used to split an array at every space character:", "var array = string.split(' ');", - "Use .split() to create an array of words from string and assign it to array." + "Use split to create an array of words from string and assign it to array." ], "tests":[ "assert(typeof(array) === 'object' && array.length === 5, 'message: You should split the string by its spaces.');", - "assert(/\\.split\\(/gi, 'message: You should use the split method on the string.');" + "assert(/\\.split\\(/gi, 'message: You should use the split method on the string.');" ], "challengeSeed":[ "var string = \"Split me into an array\";", @@ -390,25 +406,26 @@ "title":"Join Strings with .join", "difficulty":0, "description":[ - "We can use the .join() method to join each element in an array into a string separated by whatever delimiter you provide as an argument to the join operation.", - "var joinMe = joinMe.join(\" \");", - "Use the .join() method to create a string from joinMe with spaces in between each element and assign it back to joinMe." + "We can use the join method to join each element of an array into a string separated by whatever delimiter you provide as an argument.", + "The following is an example of using join to join all of the elements of an array into a string with all the elements seperated by a space:", + "var joinedString = joinMe.join(\" \");", + "Use the join method to create a string from joinMe with spaces in between each element and assign it to joinedString." ], "tests":[ - "assert(typeof(joinMe) === 'string' && joinMe === \"Split me into an array\", 'message: You should join the elements of the array with spaces.');", - "assert(/\\.join\\(/gi, 'message: You should use of the join method on the array.');" + "assert(typeof(joinedString) === 'string' && joinedString === \"Split me into an array\", 'message: You should join the elements of the array with spaces.');", + "assert(/\\.join\\(/gi, 'message: You should use of the join method on the array.');" ], "challengeSeed":[ "var joinMe = [\"Split\",\"me\",\"into\",\"an\",\"array\"];", "// Only change code below this line.", "", - "joinMe = joinMe;", + "var joinedString = joinMe;", "", "// Only change code above this line.", - "(function() {return joinMe;})();" + "(function() {return joinedString;})();" ], "MDNlinks":[ - "Array.join()" + "Array.join()" ], "challengeType":1, "type": "waypoint" From d31ce05b2a0fc3c16b14776a5919fa69aa1e0eea Mon Sep 17 00:00:00 2001 From: Michael Krebs Date: Wed, 21 Oct 2015 20:56:14 -0400 Subject: [PATCH 3/6] Adds dting's solutions to advanced bonfires --- challenges/advanced-bonfires.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/challenges/advanced-bonfires.json b/challenges/advanced-bonfires.json index 8ff025e2fe..b68dc4b0ff 100644 --- a/challenges/advanced-bonfires.json +++ b/challenges/advanced-bonfires.json @@ -57,6 +57,9 @@ "MDNlinks": [ "RegExp" ], + "solutions": [ + "var re = /^(?:(?:\\+?1\\s*(?:[.-]\\s*)?)?(?:\\(\\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\s*\\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\\s*(?:[.-]\\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\\s*(?:[.-]\\s*)?([0-9]{4})$/;\n\nfunction telephoneCheck(str) {\n return !!str.match(re);\n}\n\ntelephoneCheck(\"555-555-5555\");" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -95,6 +98,9 @@ "Array.reduce()", "Symmetric Difference" ], + "solutions": [ + "function sym(args) {\n var index = -1;\n var length = arguments.length;\n var result;\n while (++index < length) {\n var array = arguments[index];\n result = result ? diff(result, array).concat(diff(array, result)) : array;\n }\n return result ? uniq(result) : [];\n}\n\nfunction uniq(arr) {\n var h = Object.create(null);\n var u = [];\n arr.forEach(function(v) {\n if (v in h) return;\n h[v] = true;\n u.push(v);\n });\n return u;\n}\n\nfunction diff(a, b) {\n var h = Object.create(null);\n b.forEach(function(v) {\n h[v] = true; \n });\n return a.filter(function(v) { return !(v in h);});\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -151,6 +157,9 @@ "MDNlinks": [ "Global Object" ], + "solutions": [ + "var VALUES = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];\n\nfunction drawer(price, cash, cid) {\n cash = ~~(cash * 100);\n price = ~~(price * 100);\n var diff = cash-price;\n cid.forEach(function(c) {\n c[1] = ~~(c[1] * 100);\n });\n var totalCid = cid.reduce(function(a, c) {\n return a + c[1];\n }, 0);\n if (diff > totalCid) {\n return \"Insufficient Funds\";\n }\n if (diff === totalCid) {\n return \"Closed\";\n }\n \n var change = []; \n var index = cid.length;\n while (diff > 0 && --index > -1) {\n var t = 0;\n var value = VALUES[index];\n while (diff >= value && cid[index][1] > 0) {\n t += value;\n cid[index][1] -= value;\n diff -= value;\n }\n if (t) {\n change.push([cid[index][0], t/100]);\n }\n console.log(JSON.stringify(change));\n }\n // Here is your change, ma'am.\n return change;\n}\n\n// Example cash-in-drawer array:\n// [['PENNY', 1.01],\n// ['NICKEL', 2.05],\n// ['DIME', 3.10],\n// ['QUARTER', 4.25],\n// ['ONE', 90.00],\n// ['FIVE', 55.00],\n// ['TEN', 20.00],\n// ['TWENTY', 60.00],\n// ['ONE HUNDRED', 100.00]]\n\ndrawer(19.50, 20.00, [['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.10], ['QUARTER', 4.25], ['ONE', 90.00], ['FIVE', 55.00], ['TEN', 20.00], ['TWENTY', 60.00], ['ONE HUNDRED', 100.00]]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -205,6 +214,9 @@ "MDNlinks": [ "Global Array Object" ], + "solutions": [ + "function inventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // All inventory must be accounted for or you're fired!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// Example inventory lists\nvar curInv = [\n [21, 'Bowling Ball'],\n [2, 'Dirty Sock'],\n [1, 'Hair Pin'],\n [5, 'Microphone']\n];\n\nvar newInv = [\n [2, 'Hair Pin'],\n [3, 'Half-Eaten Apple'],\n [67, 'Bowling Ball'],\n [7, 'Toothpaste']\n];\n\ninventory(curInv, newInv);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -246,6 +258,9 @@ "Permutations", "RegExp" ], + "solutions": [ + "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: Global array which holds the list of permutations\n //usedChars: Global utility array which holds a list of \"currently-in-use\" characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\" from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -291,6 +306,9 @@ "String.substr()", "parseInt()" ], + "solutions": [ + "function friendly(str) {\n var dates = str.map(function(s) {return s.split('-').map(Number);});\n var start = dates[0];\n var end = dates[1];\n if (str[0] === str[1]) {\n return [readable(start)];\n }\n if (start[0] !== end[0]) {\n if (start[0] + 1 === end[0] && start[1] > end[1]) {\n start[0] = undefined;\n end[0] = undefined;\n }\n return dates.map(readable);\n }\n start[0] = undefined;\n end[0] = undefined;\n if (start[1] !== end[1]) {\n return dates.map(readable);\n }\n end[1] = undefined;\n return dates.map(readable);\n}\n\nfunction readable(arr) {\n var ordD = arr[2] + nth(arr[2]);\n if (!arr[1]) {\n return ordD;\n }\n return MONTH[arr[1]] + \" \" + ordD + (!arr[0] ? \"\" : \", \" + arr[0]);\n}\n\nvar MONTH = {1: \"January\",\n 2: \"February\",\n 3: \"March\",\n 4: \"April\",\n 5: \"May\",\n 6: \"June\",\n 7: \"July\",\n 8: \"August\",\n 9: \"September\",\n 10: \"October\",\n 11: \"November\",\n 12: \"December\"};\n\nfunction nth(d) {\n if(d>3 && d<21) return 'th';\n switch (d % 10) {\n case 1: return \"st\";\n case 2: return \"nd\";\n case 3: return \"rd\";\n default: return \"th\";\n }\n} \n\nfriendly(['2015-07-01', '2015-07-04']);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", From 529a2d8ef1d9dd38669122afada943d8a34daab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Veras?= Date: Wed, 21 Oct 2015 23:49:17 -0400 Subject: [PATCH 4/6] Updating the waypoint information After commit ec1d3ac in learnyoumongo, exercise "Find Limit" was renamed to "Find Project". --- challenges/mongodb.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/mongodb.json b/challenges/mongodb.json index 0fe93ba6f9..facfb56089 100644 --- a/challenges/mongodb.json +++ b/challenges/mongodb.json @@ -25,7 +25,7 @@ "Complete \"Mongod\"", "Complete \"Connect\"", "Complete \"Find\"", - "Complete \"Find Limit\"", + "Complete \"Find Project\"", "Complete \"Insert\"", "Complete \"Update\"", "Complete \"Remove\"", From 9a19e7e2e633d47af0f13b74a313d1123d85c438 Mon Sep 17 00:00:00 2001 From: krantzinator Date: Wed, 21 Oct 2015 21:04:33 -0400 Subject: [PATCH 5/6] added 5 solutions to basic bonfires --- challenges/basic-bonfires.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/challenges/basic-bonfires.json b/challenges/basic-bonfires.json index e2e2fa0e3f..567b90c98e 100644 --- a/challenges/basic-bonfires.json +++ b/challenges/basic-bonfires.json @@ -69,6 +69,9 @@ "Array.reverse()", "Array.join()" ], + "solutions": [ + "function reverseString(str) {\n return str.split('').reverse().join(\"\");\n}\n\nreverseString('hello');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -109,6 +112,9 @@ "MDNlinks": [ "Arithmetic Operators" ], + "solutions": [ + "function factorialize(num) {\n return num === 1 ? 1 : num * factorialize(num-1);\n}\n\nfactorialize(5);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -159,6 +165,9 @@ "String.replace()", "String.toLowerCase()" ], + "solutions": [ + "function palindrome(str) {\n var a = str.toLowerCase().replace(/[^a-z]/g, '');\n console.log(a.split('').reverse().join(''));\n return a == a.split('').reverse().join('');\n}\n\n\n\npalindrome(\"eye\");\npalindrome(\"A man, a plan, a canal. Panama\");\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -199,6 +208,9 @@ "String.split()", "String.length" ], + "solutions": [ + "function findLongestWord(str) {\n return str.split(' ').sort(function(a, b) { return b.length - a.length;})[0].length;\n}\n\nfindLongestWord('The quick brown fox jumped over the lazy dog');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -236,6 +248,9 @@ "MDNlinks": [ "String.charAt()" ], + "solutions": [ + "function titleCase(str) {\n return str.split(' ').map(function(word) {\n return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();\n }).join(' ');\n}\n\ntitleCase(\"I'm a little tea pot\");\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", From 502510ac296a59e572a4e3e59c330626659e62cc Mon Sep 17 00:00:00 2001 From: natac13 Date: Fri, 23 Oct 2015 19:18:45 -0400 Subject: [PATCH 6/6] added @dting's solutions to the intermediate bonfires closes #3837 --- challenges/intermediate-bonfires.json | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/challenges/intermediate-bonfires.json b/challenges/intermediate-bonfires.json index ea05dd355f..2fb3d14522 100644 --- a/challenges/intermediate-bonfires.json +++ b/challenges/intermediate-bonfires.json @@ -30,6 +30,9 @@ "Math.min()", "Array.reduce()" ], + "solutions": [ + "function sumAll(arr) {\n var sum = 0;\n arr.sort(function(a,b) {return a-b;});\n for (var i = arr[0]; i <= arr[1]; i++) {\n sum += i; \n }\n return sum;\n}\n\nsumAll([1, 4]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -76,6 +79,9 @@ "Array.indexOf()", "Array.concat()" ], + "solutions": [ + "function diff(arr1, arr2) {\n var newArr = [];\n var h1 = Object.create(null);\n arr1.forEach(function(e) {\n h1[e] = e;\n });\n \n var h2 = Object.create(null);\n arr2.forEach(function(e) {\n h2[e] = e;\n });\n \n Object.keys(h1).forEach(function(e) {\n if (!(e in h2)) newArr.push(h1[e]);\n });\n Object.keys(h2).forEach(function(e) {\n if (!(e in h1)) newArr.push(h2[e]);\n });\n // Same, same; but different.\n return newArr;\n}\n\ndiff([1, 2, 3, 5], [1, 2, 3, 4, 5]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -117,6 +123,9 @@ "Array.indexOf()", "Array.join()" ], + "solutions": [ + "function convert(num) {\n var ref = [['M', 1000], ['CM', 900], ['D', 500], ['CD', 400], ['C', 100], ['XC', 90], ['L', 50], ['XL', 40], ['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1]];\n var res = [];\n ref.forEach(function(p) {\n while (num >= p[1]) {\n res.push(p[0]);\n num -= p[1];\n }\n });\n return res.join('');\n}\n\nconvert(36);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -157,6 +166,9 @@ "Object.hasOwnProperty()", "Object.keys()" ], + "solutions": [ + "function where(collection, source) {\n var arr = [];\n var keys = Object.keys(source);\n collection.forEach(function(e) {\n if(keys.every(function(key) {return e[key] === source[key];})) {\n arr.push(e); \n }\n });\n return arr;\n}\n\nwhere([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -200,6 +212,9 @@ "String.replace()", "Array.join()" ], + "solutions": [ + "function replace(str, before, after) {\n if (before.charAt(0) === before.charAt(0).toUpperCase()) {\n after = after.charAt(0).toUpperCase() + after.substring(1);\n } else {\n after = after.charAt(0).toLowerCase() + after.substring(1);\n }\n return str.replace(before, after);\n}\n\nreplace(\"A quick brown fox jumped over the lazy dog\", \"jumped\", \"leaped\");\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -243,6 +258,9 @@ "String.substr()", "String.split()" ], + "solutions": [ + "function translate(str) {\n if (isVowel(str.charAt(0))) return str + \"way\";\n var front = [];\n str = str.split('');\n while (str.length && !isVowel(str[0])) {\n front.push(str.shift());\n }\n return [].concat(str, front).join('') + 'ay';\n}\n\nfunction isVowel(c) {\n return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1;\n}\n\ntranslate(\"consonant\");\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -283,6 +301,9 @@ "Array.push()", "String.split()" ], + "solutions": [ + "var lookup = Object.create(null);\nlookup.A = 'T';\nlookup.T = 'A';\nlookup.C = 'G';\nlookup.G = 'C';\n\nfunction pair(str) {\n return str.split('').map(function(p) {return [p, lookup[p]];});\n}\n\npair(\"GCG\");\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -321,6 +342,9 @@ "String.charCodeAt()", "String.fromCharCode()" ], + "solutions": [ + "function fearNotLetter(str) {\n var s = str.split('').map(function(c) {return c.charCodeAt(0);});\n for (var i = 1; i < s.length; i++) {\n if (s[i]-1 != s[i-1]) {\n return String.fromCharCode(s[i]-1);\n }\n }\n}\n\nfearNotLetter('abce');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -363,6 +387,9 @@ "MDNlinks": [ "Boolean Objects" ], + "solutions": [ + "function boo(bool) {\n // What is the new fad diet for ghost developers? The Boolean.\n return typeof(bool) === \"boolean\";\n}\n\nboo(null);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -403,6 +430,9 @@ "Arguments object", "Array.reduce()" ], + "solutions": [ + "function unite(arr1, arr2, arr3) {\n return [].slice.call(arguments).reduce(function(a, b) {\n return [].concat(a, b.filter(function(e) {return a.indexOf(e) === -1;}));\n }, []);\n}\n\nunite([1, 2, 3], [5, 2, 1, 4], [2, 1]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -444,6 +474,9 @@ "RegExp", "HTML Entities" ], + "solutions": [ + "var MAP = { '&': '&',\n '<': '<',\n '>': '>',\n '\"': '"',\n \"'\": '''};\n\nfunction convert(str) {\n return str.replace(/[&<>\"']/g, function(c) {\n return MAP[c];\n });\n}\n\nconvert('Dolce & Gabbana');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -483,6 +516,9 @@ "RegExp", "String.replace()" ], + "solutions": [ + "function spinalCase(str) {\n // \"It's such a fine line between stupid, and clever.\"\n // --David St. Hubbins\n str = str.replace(/([a-z](?=[A-Z]))/g, '$1 ');\n return str.toLowerCase().replace(/\\ |\\_/g, '-');\n}\n\nspinalCase('This Is Spinal Tap');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -523,6 +559,9 @@ "MDNlinks": [ "Remainder" ], + "solutions": [ + "function sumFibs(num) {\n var a = 1; \n var b = 1;\n var s = 0;\n while (a <= num) {\n if (a % 2 !== 0) { \n s += a; \n }\n a = [b, b=b+a][0];\n }\n return s;\n}\n\nsumFibs(4);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -561,6 +600,9 @@ "For Loops", "Array.push()" ], + "solutions": [ + "function eratosthenesArray(n) {\n var primes = [];\n if (n > 2) {\n var half = n>>1;\n var sieve = Array(half);\n for (var i = 1, limit = Math.sqrt(n)>>1; i <= limit; i++) {\n if (!sieve[i]) {\n for (var step = 2*i+1, j = (step*step)>>1; j < half; j+=step) {\n sieve[j] = true;\n }\n }\n }\n primes.push(2);\n for (var p = 1; p < half; p++) {\n if (!sieve[p]) primes.push(2*p+1);\n }\n }\n return primes;\n}\n\nfunction sumPrimes(num) {\n return eratosthenesArray(num+1).reduce(function(a,b) {return a+b;}, 0);\n}\n\nsumPrimes(10);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -600,6 +642,9 @@ "MDNlinks": [ "Smallest Common Multiple" ], + "solutions": [ + "function gcd(a, b) {\n while (b !== 0) {\n a = [b, b = a % b][0];\n }\n return a;\n}\n\nfunction lcm(a, b) {\n return (a * b) / gcd(a, b);\n}\n\nfunction smallestCommons(arr) {\n arr.sort(function(a,b) {return a-b;});\n var rng = [];\n for (var i = arr[0]; i <= arr[1]; i++) {\n rng.push(i);\n }\n return rng.reduce(lcm);\n}\n\n\nsmallestCommons([1,5]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -635,6 +680,9 @@ "MDNlinks": [ "Array.filter()" ], + "solutions": [ + "function find(arr, func) {\n var num;\n arr.some(function(e) {\n if (func(e)) {\n num = e;\n return true;\n }\n });\n return num;\n}\n\nfind([1, 2, 3, 4], function(num){ return num % 2 === 0; });\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -673,6 +721,9 @@ "Arguments object", "Array.shift()" ], + "solutions": [ + "(function drop(arr, func) {\n // Drop them elements.\n while (arr.length && !func(arr[0])) {\n arr.shift();\n }\n return arr;\n}\n\ndrop([1, 2, 3], function(n) {return n < 3; });\n)" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -710,6 +761,9 @@ "MDNlinks": [ "Array.isArray()" ], + "solutions": [ + "function steamroller(arr) {\n if (!Array.isArray(arr)) {\n return [arr];\n }\n var out = [];\n arr.forEach(function(e) {\n steamroller(e).forEach(function(v) {\n out.push(v);\n });\n });\n return out;\n}\n\nsteamroller([1, [2], [3, [[4]]]]);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -746,6 +800,9 @@ "String.charCodeAt()", "String.fromCharCode()" ], + "solutions": [ + "function binaryAgent(str) {\n return str.split(' ').map(function(s) { return parseInt(s, 2); }).map(function(b) { return String.fromCharCode(b);}).join('');\n}\n\nbinaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -785,6 +842,9 @@ "Object.hasOwnProperty()", "Object.getOwnPropertyNames()" ], + "solutions": [ + "function every(collection, pre) {\n // Does everyone have one of these?\n return collection.every(function(e) { return e[pre]; });\n}\n\nevery([{'user': 'Tinky-Winky', 'sex': 'male'}, {'user': 'Dipsy', 'sex': 'male'}, {'user': 'Laa-Laa', 'sex': 'female'}, {'user': 'Po', 'sex': 'female'}], 'sex');\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "", @@ -828,6 +888,10 @@ "Closures", "Arguments object" ], + "solutions": [ + "function add() {\n if (arguments.length == 1) {\n var a = arguments[0];\n if (!isNumber(a)) return;\n return function(b) {\n if (!isNumber(b)) return;\n return a+b;\n };\n }\n if (![].slice.call(arguments).every(isNumber)) return;\n return arguments[0] + arguments[1];\n}\n \nfunction isNumber(obj) {\n return toString.call(obj) == '[object Number]';\n}\n\nadd(2,3);\n", + "function add() {\n var a = arguments[0];\n if (toString.call(a) !== '[object Number]') return; \n if (arguments.length === 1) {\n return function(b) {\n if (toString.call(b) !== '[object Number]') return;\n return a + b;\n };\n }\n var b = arguments[1];\n if (toString.call(b) !== '[object Number]') return; \n return a + arguments[1];\n}\n\nadd(2,3);\n" + ], "type": "bonfire", "challengeType": 5, "nameCn": "",