diff --git a/app/arrays.js b/app/arrays.js index 5e3fdce..666215a 100644 --- a/app/arrays.js +++ b/app/arrays.js @@ -16,6 +16,11 @@ exports.arraysAnswers = { }, sum : function(arr) { + + /* + if (Array.prototype.reduce) { return arr.reduce( (base, el) => base + el, 0); } + */ + var sum = 0; for (var i = 0, len = arr.length; i < len; i++) { @@ -26,6 +31,11 @@ exports.arraysAnswers = { }, remove : function(arr, item) { + + /* + if (Array.prototype.filter) { return arr.filter((el) => !(el === item)); } + */ + var ret = []; for (var i = 0, len = arr.length; i < len; i++) { @@ -38,6 +48,16 @@ exports.arraysAnswers = { }, removeWithoutCopy : function(arr, item) { + + /* + for ( var pos = arr.indexOf(item) + ; pos > -1 + ; pos = arr.indexOf(item)) { + arr.splice(pos, 1); + } + return arr; + */ + var i, len; for (i = 0, len = arr.length; i < len; i++) { @@ -81,6 +101,14 @@ exports.arraysAnswers = { }, count : function(arr, item) { + + /* + if (Array.prototype.reduce) { + return arr.reduce((base, el) => base + (el === item ? 1 : 0) + , 0); + } + */ + var count = 0; for (var i = 0, len = arr.length; i < len; i++) { @@ -93,6 +121,18 @@ exports.arraysAnswers = { }, duplicates : function(arr) { + + /* + var seen = new Set([]), + dupes = new Set([]); + arr.forEach(el => { + if (seen.has(el)) {dupes.add(el);} + seen.add(el); + } + ); + return Array.from(dupes); + */ + var seen = {}; var dupes = []; @@ -110,6 +150,13 @@ exports.arraysAnswers = { }, square : function(arr) { + + /* + if (Array.prototype.map) { + return arr.map(el => el * el); + } + */ + var ret = []; for (var i = 0, len = arr.length; i < len; i++) { diff --git a/app/async.js b/app/async.js index a3a0bbd..7c9e33e 100644 --- a/app/async.js +++ b/app/async.js @@ -1,7 +1,13 @@ exports = (typeof window === 'undefined') ? global : window; exports.asyncAnswers = { + async : function(value) { + + /* does not use jQuery + return new Promise((resolve, reject) => setTimeout(() => resolve(value), 10)); + */ + var dfd = $.Deferred(); setTimeout(function() { dfd.resolve(value); diff --git a/app/flowControl.js b/app/flowControl.js index 67dcda6..998cb85 100644 --- a/app/flowControl.js +++ b/app/flowControl.js @@ -21,7 +21,7 @@ exports.flowControlAnswers = { // if the number is divisible by 3 AND 5, then when divided by both, // the remainder for each operation will be zero // return 'fizzbuzz' - if (num % 3 === 0 && num % 5 === 0) { + if (num % (3*5) === 0) { return 'fizzbuzz'; } diff --git a/app/functions.js b/app/functions.js index 2f0249d..c9dd125 100644 --- a/app/functions.js +++ b/app/functions.js @@ -1,11 +1,19 @@ +purge + exports = (typeof window === 'undefined') ? global : window; exports.functionsAnswers = { argsAsArray : function(fn, arr) { + /* + return fn(...arr); + */ return fn.apply(null, arr); }, speak : function(fn, obj) { + /* + return fn.bind(obj)(); + */ return fn.call(obj); }, @@ -16,6 +24,15 @@ exports.functionsAnswers = { }, makeClosures : function(arr, fn) { + + /* + if (Array.prototype.map) { + return arr.map(function (el) { + return function () {return fn(el);}; + }); + } + */ + var ret = []; var makeFn = function(val) { @@ -34,6 +51,14 @@ exports.functionsAnswers = { return fn.call(null, str1, str2, str3); }; }, + + /* + useArguments: function (...ar) { + if (Array.prototype.reduce) { + return ar.reduce((el, base) => el + base); + } + }, + */ useArguments : function() { var sum = 0; @@ -45,10 +70,24 @@ exports.functionsAnswers = { return sum; }, + /* + callIt: function (fn, ...ar) { + return fn(...ar); + }, + */ + callIt : function(fn) { var args = Array.prototype.slice.call(arguments, 1, arguments.length); fn.apply(null, args); }, + + /* + partialUsingArguments: function (fn, ...ar) { + return function (...br) { + return fn(...ar, ...br); + }; + }, + */ partialUsingArguments : function(fn) { var args = Array.prototype.slice.call(arguments, 1, arguments.length); @@ -59,6 +98,23 @@ exports.functionsAnswers = { }, curryIt : function(fn) { + + /* + var allargs = [], + funcLen = fn.length; + + var f = function (newarg) { + if (newarg !== undefined && newarg != null) { + allargs.push(newarg); + if (allargs.length >= funcLen) { + return fn(...allargs); + } + } + return f; + }; + return f(null); + */ + function applyArguments(fn, arguments) { return fn.apply(null, arguments); } diff --git a/app/recursion.js b/app/recursion.js index 27d4b32..e5e7f3e 100644 --- a/app/recursion.js +++ b/app/recursion.js @@ -1,7 +1,27 @@ exports = (typeof window === 'undefined') ? global : window; exports.recursionAnswers = { + listFiles: function(data, dirName) { + + /* + // thisdir: does the dir of the data contribute to the answer? + var thisdir = (dir === undefined) || (dir === null) || (data.dir === dir), + rslt = [], + elval; + for ( var el in data.files) { + // elements in 'files' (elval) can be filename (a string) or object + elval = data.files[el]; + if (typeof elval === 'string') { + if (thisdir) { rslt.push(elval); } + } else { + // recursion + rslt = rslt.concat(this.listFiles(elval, thisdir ? elval.dir : dir)); + } + } + return rslt; + */ + var listOfFiles = []; var dirs = []; @@ -72,5 +92,41 @@ exports.recursionAnswers = { temp.slice() ); } + }, + + fibonacci: function (n) { + // a is the previous-previous number in the series + // b is the previous number (latest) in the series + // q is which number in the series to return + var fib = function (a, b, q) { + if (q === 0) { return a; } + if (q === 1) { return b; } + // recursion + return fib(b, a + b, q - 1); + }; + return fib(0, 1, n); + }, + + validParentheses: function (n) { + var vParens = function (nInPar, nOutPar) { + var result = []; + var theRest = []; + // terminal case + if (nOutPar === 0) { return ['']; } + if (nInPar > 0) { + // can output '(' + // recursion + theRest = vParens(nInPar - 1, nOutPar); + theRest.forEach((el) => result.push('('.concat(el))); + } + if (nOutPar > nInPar) { + // can output ')' + // recursion + theRest = vParens(nInPar, nOutPar - 1); + theRest.forEach((el) => result.push(')'.concat(el))); + } + return result; + }; + return vParens(n, n); } }; diff --git a/app/strings.js b/app/strings.js new file mode 100644 index 0000000..40e67f8 --- /dev/null +++ b/app/strings.js @@ -0,0 +1,47 @@ +exports = (typeof window === 'undefined') ? global : window; + +exports.stringsAnswers = { + reduceString: function (str, amount) { + var track = { char: '', cnt: 0 }; + var rsltArray = str.split('').filter(function (el) { + if (el === track.char) { + if (track.cnt < amount) { + track.cnt++; + return true; + } else { return false; } + } else { + track = { char: el, cnt: 1 }; + return true; + } + } + ); + return rsltArray.join(''); + }, + + wordWrap: function (str, cols) { + var colCnt = 0, + currWord, + i, + prevWord, + resultArray = [], + wordArray = str.split(/\s/), + wordArrayLen = wordArray.length; + prevWord = wordArray[0]; + for (i = 1; i < wordArrayLen; i++, prevWord = currWord) { + currWord = wordArray[i]; + if ((colCnt + prevWord.length + currWord.length + 1) <= cols) { + resultArray.push(prevWord + ' '); + colCnt += prevWord.length + 1; + } else { + resultArray.push(prevWord + '\n'); + colCnt = 0; + } + } + resultArray.push(prevWord); + return resultArray.join(''); + }, + + reverseString: function (str) { + return str.split('').reverse().join(''); + } +};