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

Commit

Permalink
Merge pull request #35 from rmurphey/refactor
Browse files Browse the repository at this point in the history
update solutions to match new exports pattern after removing require
  • Loading branch information
ashleygwilliams committed May 13, 2015
2 parents 0de3fff + 42d212b commit fb5e3ae
Show file tree
Hide file tree
Showing 13 changed files with 419 additions and 477 deletions.
214 changes: 106 additions & 108 deletions app/arrays.js
Original file line number Diff line number Diff line change
@@ -1,135 +1,133 @@
if (typeof define !== 'function') { var define = require('amdefine')(module); }

define(function() {
return {
indexOf : function(arr, item) {
/*
if (Array.prototype.indexOf) { return arr.indexOf(item); }
*/

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
return i;
}
exports = (typeof window === 'undefined') ? global : window;

exports.arraysAnswers = {
indexOf : function(arr, item) {
/*
if (Array.prototype.indexOf) { return arr.indexOf(item); }
*/

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
return i;
}
}

return -1;
},
return -1;
},

sum : function(arr) {
var sum = 0;
sum : function(arr) {
var sum = 0;

for (var i = 0, len = arr.length; i < len; i++) {
sum += arr[i];
}
for (var i = 0, len = arr.length; i < len; i++) {
sum += arr[i];
}

return sum;
},
return sum;
},

remove : function(arr, item) {
var ret = [];
remove : function(arr, item) {
var ret = [];

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] !== item) {
ret.push(arr[i]);
}
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] !== item) {
ret.push(arr[i]);
}
}

return ret;
},
return ret;
},

removeWithoutCopy : function(arr, item) {
var i, len;
removeWithoutCopy : function(arr, item) {
var i, len;

for (i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
arr.splice(i, 1);
i = i - 1;
len = len - 1;
}
for (i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
arr.splice(i, 1);
i = i - 1;
len = len - 1;
}
}

return arr;
},

append : function(arr, item) {
arr.push(item);
return arr;
},

truncate : function(arr) {
arr.pop();
return arr;
},

prepend : function(arr, item) {
arr.unshift(item);
return arr;
},

curtail : function(arr) {
arr.shift(arr);
return arr;
},

concat : function(arr1, arr2) {
return arr1.concat(arr2);
},

insert : function(arr, item, index) {
arr.splice(index, 0, item);
return arr;
},

count : function(arr, item) {
var count = 0;

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
count++;
}
}
return arr;
},

return count;
},
append : function(arr, item) {
arr.push(item);
return arr;
},

duplicates : function(arr) {
var seen = {};
var dupes = [];
truncate : function(arr) {
arr.pop();
return arr;
},

for (var i = 0, len = arr.length; i < len; i++) {
seen[arr[i]] = seen[arr[i]] ? seen[arr[i]] + 1 : 1;
}
prepend : function(arr, item) {
arr.unshift(item);
return arr;
},

for (var item in seen) {
if (seen.hasOwnProperty(item) && seen[item] > 1) {
dupes.push(item);
}
}
curtail : function(arr) {
arr.shift(arr);
return arr;
},

concat : function(arr1, arr2) {
return arr1.concat(arr2);
},

return dupes;
},
insert : function(arr, item, index) {
arr.splice(index, 0, item);
return arr;
},

square : function(arr) {
var ret = [];
count : function(arr, item) {
var count = 0;

for (var i = 0, len = arr.length; i < len; i++) {
ret.push(arr[i] * arr[i]);
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
count++;
}
}

return ret;
},
return count;
},

findAllOccurrences : function(arr, target) {
var ret = [];
duplicates : function(arr) {
var seen = {};
var dupes = [];

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === target) {
ret.push(i);
}
for (var i = 0, len = arr.length; i < len; i++) {
seen[arr[i]] = seen[arr[i]] ? seen[arr[i]] + 1 : 1;
}

for (var item in seen) {
if (seen.hasOwnProperty(item) && seen[item] > 1) {
dupes.push(item);
}
}

return ret;
return dupes;
},

square : function(arr) {
var ret = [];

for (var i = 0, len = arr.length; i < len; i++) {
ret.push(arr[i] * arr[i]);
}
};
});

return ret;
},

findAllOccurrences : function(arr, target) {
var ret = [];

for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i] === target) {
ret.push(i);
}
}

return ret;
}
};
40 changes: 19 additions & 21 deletions app/async.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
if (typeof define !== 'function') { var define = require('amdefine')(module); }
exports = (typeof window === 'undefined') ? global : window;

define([ 'jquery' ], function($) {
return {
async : function(value) {
var dfd = $.Deferred();
setTimeout(function() {
dfd.resolve(value);
}, 10);
return dfd.promise();
},
exports.asyncAnswers = {
async : function(value) {
var dfd = $.Deferred();
setTimeout(function() {
dfd.resolve(value);
}, 10);
return dfd.promise();
},

manipulateRemoteData : function(url) {
var dfd = $.Deferred();
manipulateRemoteData : function(url) {
var dfd = $.Deferred();

$.ajax(url).then(function(resp) {
var people = $.map(resp.people, function(person) {
return person.name;
});
dfd.resolve(people.sort());
$.ajax(url).then(function(resp) {
var people = $.map(resp.people, function(person) {
return person.name;
});
dfd.resolve(people.sort());
});

return dfd.promise();
}
};
});
return dfd.promise();
}
};
50 changes: 24 additions & 26 deletions app/bestPractices.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
if (typeof define !== 'function') { var define = require('amdefine')(module); }
exports = (typeof window === 'undefined') ? global : window;

/**
* This file defines an object with some methods. Some of these methods are
* populated incorrectly; your job is to fix them. Other methods are not
* populated at all; your job is to fill them out.
*/
define(function() {
return {
exports.bestPracticesAnswers = {

globals : function() {
var myObject = {
name : 'Jory'
};
globals : function() {
var myObject = {
name : 'Jory'
};

return myObject;
},
return myObject;
},

functions : function(flag) {
var getValue;
functions : function(flag) {
var getValue;

if (flag) {
getValue = function() { return "a"; }
} else {
getValue = function() { return "b"; }
}
if (flag) {
getValue = function() { return "a"; }
} else {
getValue = function() { return "b"; }
}

return getValue();
},
return getValue();
},

parseInt : function(num) {
return parseInt(num, 10);
},
parseInt : function(num) {
return parseInt(num, 10);
},

identity : function(val1, val2) {
return val1 === val2;
}
};
});
identity : function(val1, val2) {
return val1 === val2;
}
};
32 changes: 0 additions & 32 deletions app/config.js

This file was deleted.

Loading

0 comments on commit fb5e3ae

Please sign in to comment.