-
Notifications
You must be signed in to change notification settings - Fork 82
intermediate javascript exercise sols #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 11 commits
e020f7b
abfac49
5698a00
c1b0bc7
f6b21f4
b4d5310
a253fe5
6bd6d7b
26867d4
317c8c8
2cd69a8
38f8f15
d7d53b6
58f3863
ab7f4a1
a17aad4
3fefe46
c098e3d
3e0dffe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
||
| 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 | ||
|
||
| 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(){ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need an
elsehereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.