Merge branch 'staging' of https://github.com/freecodecamp/freecodecamp into staging
This commit is contained in:
@ -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": "",
|
||||
|
@ -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": "",
|
||||
|
@ -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": "",
|
||||
@ -763,14 +820,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 <a href='//github.com/FreeCodeCamp/freecodecamp/wiki/How-to-get-help-when-you-get-stuck' target='_blank'>Read-Search-Ask</a> 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;",
|
||||
"}",
|
||||
"",
|
||||
@ -778,12 +834,17 @@
|
||||
],
|
||||
"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: <code>every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\")</code> 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: <code>every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"})</code> 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: <code>every([{\"user\": \"Tinky-Winky\", \"sex\": \"female\"}, {\"user\": \"Dipsy\", \"sex\": \"male\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], {\"sex\": \"female\"})</code> should return false.');"
|
||||
"assert.strictEqual(every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\"), false, 'message: <code>every([{\"user\": \"Tinky-Winky\", \"sex\": \"male\"}, {\"user\": \"Dipsy\"}, {\"user\": \"Laa-Laa\", \"sex\": \"female\"}, {\"user\": \"Po\", \"sex\": \"female\"}], \"sex\")</code> 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: <code>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\")</code> should return false.');",
|
||||
"assert.strictEqual(every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true}, {\"name\": \"FastFoward\", \"onBoat\": null}], \"onBoat\"), false, 'message: <code>every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true}, {\"name\": \"FastFoward\", \"onBoat\": null}], \"onBoat\")</code> should return false');",
|
||||
"assert.strictEqual(every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true, \"alias\": \"Repete\"}, {\"name\": \"FastFoward\", \"onBoat\": true}], \"onBoat\"), true, 'message: <code>every([{\"name\": \"Pete\", \"onBoat\": true}, {\"name\": \"Repeat\", \"onBoat\": true, \"alias\": \"Repete\"}, {\"name\": \"FastFoward\", \"onBoat\": true}], \"onBoat\")</code> should return true');",
|
||||
"assert.strictEqual(every([{\"single\": \"yes\"}], \"single\"), true, 'message: <code>every([{\"single\": \"yes\"}], \"single\")</code> should return true');",
|
||||
"assert.strictEqual(every([{\"single\": \"\"}, {\"single\": \"double\"}], \"single\"), false, 'message: <code>every([{\"single\": \"\"}, {\"single\": \"double\"}], \"single\")</code> should return false');",
|
||||
"assert.strictEqual(every([{\"single\": \"double\"}, {\"single\": undefined}], \"single\"), false, 'message: <code>every([{\"single\": \"double\"}, {\"single\": undefined}], \"single\")</code> should return false');",
|
||||
"assert.strictEqual(every([{\"single\": \"double\"}, {\"single\": NaN}], \"single\"), false, 'message: <code>every([{\"single\": \"double\"}, {\"single\": NaN}], \"single\")</code> should return false');"
|
||||
],
|
||||
"MDNlinks": [
|
||||
"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,
|
||||
@ -828,6 +889,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": "",
|
||||
|
@ -25,7 +25,7 @@
|
||||
"Complete \"Mongod\"",
|
||||
"Complete \"Connect\"",
|
||||
"Complete \"Find\"",
|
||||
"Complete \"Find Limit\"",
|
||||
"Complete \"Find Project\"",
|
||||
"Complete \"Insert\"",
|
||||
"Complete \"Update\"",
|
||||
"Complete \"Remove\"",
|
||||
|
@ -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.",
|
||||
"<code>array = array.map(function(val){</code>",
|
||||
"<code>  return val+1;</code>",
|
||||
"The <code>map</code> method is a convenient way to iterate through arrays. Here's an example usage:",
|
||||
"<code>var timesFour = array.map(function(val){</code>",
|
||||
"<code>  return val*4;</code>",
|
||||
"<code>});</code>",
|
||||
"",
|
||||
"The <code>map</code> 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 <code>val</code> argument) but your callback can also include arguments for the <code>index</code> and <code>array</code> being acted on.",
|
||||
"Use the map function to add 3 to every value in the variable <code>array</code>."
|
||||
],
|
||||
"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 <code>.map</code>.');"
|
||||
"assert(editor.getValue().match(/\\.map\\s*\\(/gi), 'message: You should be making use of the <code>map</code> method.');",
|
||||
"assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\]/gi), 'message: You should only modify the array with <code>map</code>.');"
|
||||
],
|
||||
"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 <code>reduce</code> is used to iterate through an array and condense it into one value.",
|
||||
"To use <code>reduce</code> you pass in a callback whose arguments are an accumulator (in this case, <code>previousVal</code>) and the current value (<code>currentVal</code>).",
|
||||
"<code>reduce</code> 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 <code>reduce</code> being used to sum all the values of an array:",
|
||||
"<code>var singleVal = array.reduce(function(previousVal, currentVal){</code>",
|
||||
"<code>  return previousVal+currentVal;</code>",
|
||||
"<code>});</code>",
|
||||
"Use the <code>reduce</code> function to sum all the values in <code>array</code> and assign it to <code>singleVal</code>."
|
||||
"Use the <code>reduce</code> method to sum all the values in <code>array</code> and assign it to <code>singleVal</code>."
|
||||
],
|
||||
"tests":[
|
||||
"assert(singleVal == 30, 'message: <code>singleVal</code> should be equal to the sum of all items in the <code>array</code> 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 <code>reduce</code> method.');"
|
||||
],
|
||||
"challengeSeed":[
|
||||
"var array = [4,5,6,7,8];",
|
||||
@ -242,16 +247,19 @@
|
||||
"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 <code>filter</code> method is used to iterate through an array and filter out elements where a given condition is not true.",
|
||||
"<code>filter</code> is passed a callback function which takes the current value (we've called that <code>val</code>) as an argument. It can also use arguments for the <code>index</code> and <code>array</code> 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:",
|
||||
"<code>array = array.filter(function(val) {</code>",
|
||||
"<code>  return val <= 5;</code>",
|
||||
"<code>});</code>"
|
||||
"<code>  return val % 2 === 0;</code>",
|
||||
"<code>});</code>",
|
||||
"Use <code>filter</code> to remove all elements from <code>array</code> 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 <code>.filter</code> to modify the contents of the array.');"
|
||||
"assert(editor.getValue().match(/array\\.filter\\s*\\(/gi), 'message: You should be using the <code>filter</code> 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 <code>filter</code> to modify the contents of the array.');"
|
||||
],
|
||||
"challengeSeed":[
|
||||
"var array = [1,2,3,4,5,6,7,8,9,10];",
|
||||
@ -273,16 +281,20 @@
|
||||
"title": "Sort Arrays with .sort",
|
||||
"difficulty":0,
|
||||
"description":[
|
||||
"You can use the method <code>sort</code> to easily sort the values in the array alphabetically or numerically.",
|
||||
"<code>var array = [1, 3, 2];</code>",
|
||||
"<code>array = array.sort();</code>",
|
||||
"<code>array</code> is now <code>[1, 2, 3]</code>.",
|
||||
"You can use the method <code>sort</code> to easily sort the values in an array alphabetically or numerically.",
|
||||
"Unlike the previous array methods we have been looking at, <code>sort</code> actually alters the array in place. However, it also returns this sorted array.",
|
||||
"<code>sort</code> 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:",
|
||||
"<code>var array = [1, 12, 21, 2];</code>",
|
||||
"<code>array.sort(function(a, b) {</code>",
|
||||
"<code>  return a - b;</code>",
|
||||
"<code>});</code>",
|
||||
"Use <code>sort</code> to sort <code>array</code> 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 <code>.sort</code> 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 <code>sort</code> to modify the array.');",
|
||||
"assert(editor.getValue().match(/\\.sort\\s*\\(\\)/gi), 'message: You should have made use of the <code>sort</code> method.');"
|
||||
],
|
||||
"challengeSeed":[
|
||||
"var array = ['beta', 'alpha', 'charlie'];",
|
||||
@ -303,7 +315,8 @@
|
||||
"id": "cf1111c1c16feddfaeb2bdef",
|
||||
"title": "Reverse Arrays with .reverse",
|
||||
"description": [
|
||||
"You can use the <code>reverse</code> function to reverse the contents of an array.",
|
||||
"You can use the <code>reverse</code> method to reverse the elements of an array.",
|
||||
"<code>reverse</code> is another array method that alters the array in place, but it also returns the reversed array.",
|
||||
"Add a line of code that uses <code>reverse</code> to reverse the <code>array</code> variable."
|
||||
],
|
||||
"tests": [
|
||||
@ -330,12 +343,14 @@
|
||||
"id": "cf1111c1c16feddfaeb3bdef",
|
||||
"title": "Concatenate Strings with .concat",
|
||||
"description": [
|
||||
"<code>.concat()</code> can be used to merge the contents of two arrays into one.",
|
||||
"<code>concat</code> can be used to merge the contents of two arrays into one.",
|
||||
"<code>concat</code> 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 <code>concat</code> being used to concatenate <code>otherArray</code> onto the end of <code>array</code>:",
|
||||
"<code>array = array.concat(otherArray);</code>",
|
||||
"Use <code>.concat()</code> to concatenate <code>concatMe</code> onto the end of <code>array</code> and assign it back to array."
|
||||
"Use <code>.concat()</code> to concatenate <code>concatMe</code> onto the end of <code>array</code> and assign it back to <code>array</code>."
|
||||
],
|
||||
"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 <code>concat</code> 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 <code>concat</code> to modify the arrays.');"
|
||||
],
|
||||
@ -361,14 +376,15 @@
|
||||
"title":"Split Strings with .split",
|
||||
"difficulty":0,
|
||||
"description":[
|
||||
"You can use the <code>.split()</code> method to split a string into an array.",
|
||||
"<code>.split()</code> uses the argument you pass in as a delimiter to determine which points the string should be split at.",
|
||||
"You can use the <code>split</code> method to split a string into an array.",
|
||||
"<code>split</code> uses the argument you pass in as a delimiter to determine which points the string should be split at.",
|
||||
"Here is an example of <code>split</code> being used to split an array at every space character:",
|
||||
"<code>var array = string.split(' ');</code>",
|
||||
"Use <code>.split()</code> to create an array of words from <code>string</code> and assign it to <code>array</code>."
|
||||
"Use <code>split</code> to create an array of words from <code>string</code> and assign it to <code>array</code>."
|
||||
],
|
||||
"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 <code>split</code> method on the string.');"
|
||||
],
|
||||
"challengeSeed":[
|
||||
"var string = \"Split me into an array\";",
|
||||
@ -390,22 +406,23 @@
|
||||
"title":"Join Strings with .join",
|
||||
"difficulty":0,
|
||||
"description":[
|
||||
"We can use the <code>.join()</code> method to join each element in an array into a string separated by whatever delimiter you provide as an argument to the join operation.",
|
||||
"<code>var joinMe = joinMe.join(\" \");</code>",
|
||||
"Use the <code>.join()</code> method to create a string from <code>joinMe</code> with spaces in between each element and assign it back to <code>joinMe</code>."
|
||||
"We can use the <code>join</code> 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 <code>join</code> to join all of the elements of an array into a string with all the elements seperated by a space:",
|
||||
"<code>var joinedString = joinMe.join(\" \");</code>",
|
||||
"Use the <code>join</code> method to create a string from <code>joinMe</code> with spaces in between each element and assign it to <code>joinedString</code>."
|
||||
],
|
||||
"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 <code>join</code> 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()"
|
||||
|
Reference in New Issue
Block a user