Skip to content

Instantly share code, notes, and snippets.

View martin-mok's full-sized avatar

Martin martin-mok

  • /dev/null
View GitHub Profile
@martin-mok
martin-mok / combinators.js
Created January 7, 2020 01:09 — forked from Avaq/combinators.js
Common combinators in JavaScript
const I = x => x;
const K = x => y => x;
const A = f => x => f(x);
const T = x => f => f(x);
const W = f => x => f(x)(x);
const C = f => y => x => f(x)(y);
const B = f => g => x => f(g(x));
const S = f => g => x => f(x)(g(x));
const P = f => g => x => y => f(g(x))(g(y));
const Y = f => (g => g(g))(g => f(x => g(g)(x)));
@martin-mok
martin-mok / jsFilter.md
Last active January 6, 2020 23:07
Implement the filter Method on a Prototype

My soln:

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line
this.forEach(
  item=>
@martin-mok
martin-mok / nativeJavaScript.js
Created January 6, 2020 22:49 — forked from alexhawkins/nativeJavaScript.js
Implementation of Native JavaScript Methods (forEach, Map, Filter, Reduce, Every, Some)
'use strict';
/*****************NATIVE forEACH*********************/
Array.prototype.myEach = function(callback) {
for (var i = 0; i < this.length; i++)
callback(this[i], i, this);
};
//tests
@martin-mok
martin-mok / filterToExtractData.md
Last active January 6, 2020 22:38
freecodecamp Functional Programming: Use the filter Method to Extract Data from an Array

The variable watchList holds an array of objects with information on several movies.
Use a combination of filter and map on watchList to assign a new array of objects
with only title and rating keys. The new array should only include objects where
imdbRating is greater than or equal to 8.0.
Note that the rating values are saved as strings in the object
and you may need to convert them into numbers to perform mathematical operations on them.

My slon:

// the global variable
@martin-mok
martin-mok / myMap.md
Last active January 6, 2020 22:09
Implement javascript array map method

Functional Programming: Implement map on a Prototype

Write your own Array.prototype.myMap(), which should behave exactly
like Array.prototype.map(). You may use a for loop or the forEach method.
My first attempt:

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
 var newArray = [];
@martin-mok
martin-mok / array_splice.html
Created January 6, 2020 16:46 — forked from amoilanen/array_splice.html
Implementation of Array.prototype.splice
<!DOCTYPE html>
<html>
<head>
<title>Array.splice Implementation</title>
<link rel="stylesheet" type="text/css" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" />
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript">
if (!Array.prototype.splice2) {
Array.prototype.splice2 = function(index, howmany) {
if (index < 0) {
@martin-mok
martin-mok / MathJax.ipynb
Created January 5, 2020 23:54 — forked from cyhsutw/MathJax.ipynb
Grabbed from https://github.com/odewahn/ipynb-examples, converted to v3 for GitHub to render.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@martin-mok
martin-mok / chunkArrayInGroups.js
Created January 2, 2020 00:27
freecodecamp: Basic Algorithm Scripting: Chunky Monkey
/*
Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
tests:
- text: <code>chunkArrayInGroups(["a", "b", "c", "d"], 2)</code> should return <code>[["a", "b"], ["c", "d"]]</code>.
testString: assert.deepEqual(chunkArrayInGroups(["a", "b", "c", "d"], 2), [["a", "b"], ["c", "d"]]);
- text: <code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)</code> should return <code>[[0, 1, 2], [3, 4, 5]]</code>.
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3), [[0, 1, 2], [3, 4, 5]]);
- text: <code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)</code> should return <code>[[0, 1], [2, 3], [4, 5]]</code>.
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2), [[0, 1], [2, 3], [4, 5]]);
@martin-mok
martin-mok / checkingChars.js
Created January 1, 2020 21:39
freecodecamp: Basic Algorithm Scripting: Mutations
/*
Basic Algorithm Scripting: Mutations
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
@martin-mok
martin-mok / IndexToInsertInSortedNumberArray.js
Created January 1, 2020 20:35
freecodecamp: Basic Algorithm Scripting: Where do I Belong. Return index for insertion in sorted number array
/*
Basic Algorithm Scripting: Where do I Belong
Return the lowest index at which a value (second argument) should be inserted into an array
(first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted
it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
*/
//My soln:
function getIndexToIns(arr, num) {