Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Master #107

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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 = [];

Expand All @@ -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++) {
Expand Down
6 changes: 6 additions & 0 deletions app/async.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/flowControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
56 changes: 56 additions & 0 deletions app/functions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
purge

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why that 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);
},

Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
56 changes: 56 additions & 0 deletions app/recursion.js
Original file line number Diff line number Diff line change
@@ -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 = [];

Expand Down Expand Up @@ -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);
}
};
47 changes: 47 additions & 0 deletions app/strings.js
Original file line number Diff line number Diff line change
@@ -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('');
}
};