Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
219 changes: 199 additions & 20 deletions lodash_exercise/lodash.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,240 @@
function drop(){
function drop(arr, n=1){
var nArr = [];
for(var i=n; i<arr.length; i++)
nArr.push(arr[i]);

return nArr;
}

function fromPairs(){
function fromPairs(arr){
var nObj = {};
for(var i=0; i<arr.length; i++)
nObj[arr[i][0]] = arr[i][1];

return nObj;
}

function head(){
function head(arr){
if(arr === [])
return undefined;

}

function take(){
return arr[0];

}

function takeRight(){
function take(arr, num=1){
var nArr = [];
for(var i=0; i<num; i++) {
if(i === arr.length)
break;

nArr.push(arr[i]);
}
return nArr;
}

function union(){
// Implementation 1
function takeRight(arr, num=1){
var nArr = [];
for(var i=0; i<num; i++) {
if(i === arr.length)
break;

nArr.push(arr[(arr.length-1) - i]);
}
return nArr.reverse();
}

function zipObject(){
// Implementation 2
function takeRight(arr, num=1){
var nArr = [];
while (num > arr.length) {
num--;
}

}

function includes(){
nArr = arr.slice(arr.length - num);

return nArr;
}

function sample(){
function union(...arrays){
var rSet = new Set();
var nArr = [];

}
for(var i=0; i<arrays.length; i++)
nArr = nArr.concat(arrays[i]);

function cloneDeep(){
for (var j=0; j<nArr.length; j++) {
rSet.add(nArr[j]);
}

return Array.from(rSet);
}

function sumBy(){
function zipObject(props, values){
var nObj = {};
for(var i=0; i<props.length; i++) {
if (i > values.length -1)
nObj[props[i]] = undefined;

nObj[props[i]] = values[i];
}
return nObj;
}

function inRange(){
function includes(a, val, idx=0){
if (typeof a === 'string') {
if(a.indexOf(val) !== -1)
return true;
}
else if (Array.isArray(a)) {
if (idx > 0) {
var nArr = a.slice(idx);
if(nArr.indexOf(val) > -1)
return true;
}
else {
if(a.indexOf(val) > -1)
return true;
}
}
else if (typeof a === 'object') {
if (Object.values(a).indexOf(val) > -1)
return true;
}
else if (Array.isArray(a)) {
if (idx > 0) {
var nArr = a.slice(idx);
if(nArr.indexOf(val) > -1)
return true;
}
else
if(a.indexOf(val) > -1)
return true;
}

return false;
}

function sample(arr){
var num = Math.floor(Math.random() * arr.length);
return arr[num];
}

function has(){
function cloneDeep(a){
if (Array.isArray(a)) {
var nArr = [];
for (var i=0; i < a.length; i++) {
if (Array.isArray(a[i]) || typeof a[i] === 'object')
nArr.push(cloneDeep(a[i]));
else
nArr.push(a[i]);
}

return nArr;
}
else if (typeof a === 'object') {
var nObj = {};
for (key in a) {
if ( Array.isArray(a[key]) || typeof a[key] === 'object')
nObj[key] = cloneDeep(a[key]);
}
return nObj;
}
}

function sumBy(arr, second){
var sum = 0;
if(typeof second === 'function') {
for(var i=0; i<arr.length; i++)
sum += second(arr[i]);
}
else if(typeof second === 'string') {
for(var i=0; i<arr.length; i++)
sum += arr[i][second];
}

return sum;
}

function omit(){
function inRange(num, start=0, end){

if (start != 0 && end === undefined) {
end = start
start = 0;
}

if (num < 0)
return num > end && num < start;
else
return num < end && num > start;
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need an else here

Copy link
Author

Choose a reason for hiding this comment

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

  1. indentation changed to 2 spaces on my editor.
  2. I tried taking off the else statement here, re-ran tests and it causes all 'inRange' tests to fail except: 'should handle negative numbers as well'

}

function has(obj, gKeys){
if(typeof gKeys === 'string')
gKeys = gKeys.split('.');

var presentKeys = [];
function helper(a) {
for (key in a) {
presentKeys.push(key);

if(typeof a[key] === 'object')
helper(a[key]);
else
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't need an else return here since our function will end after these lines

Copy link
Author

Choose a reason for hiding this comment

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

have taken out else statement here. will be checked in the next checkin.

return;
}
}
helper(obj);

for(var i=0; i<gKeys.length; i++) {
if (presentKeys.indexOf(gKeys[i]) === -1)
return false;
}

return true;
}

function pick(){
function omit(obj, gKeys){
if(typeof gKeys === 'string')
gKeys = gKeys.split('.');

var rObj = {};
function helper(a) {
for (key in a) {
if (gKeys.indexOf(key) === -1) {
if (a[key] !== undefined)
if(typeof a[key] === 'object')
helper(a[key]);
else
rObj[key] = a[key];
}
}
}
helper(obj);

return rObj;
}

function pick(obj, gKeys){
if(typeof gKeys === 'string')
gKeys = gKeys.split('.');

var rObj = {};
function helper(a) {
for (key in a) {
if (gKeys.indexOf(key) !== -1) {
if (a[key] !== undefined)
if(typeof a[key] === 'object')
helper(a[key]);
else
rObj[key] = a[key];
}
}
}
helper(obj);

return rObj;
}

function pickBy(){
Expand Down
Loading