Skip to content

Instantly share code, notes, and snippets.

@kephin
kephin / exercise_after_class_1.js
Last active July 16, 2017 08:48
課後練習題
(function(){ var a = b = 3; })();
//此function等同於
(function(){
var a = 3;
b = 3;
})();
// 所以此IIFE執行完後,在function內宣告的變數a會立即被回收
a; // -> Error: not defined
// 但是沒有宣告b,所以b是window內的全域屬性
@kephin
kephin / exercise_6.js
Last active July 16, 2017 08:51
利用 JS 閉包 (Closure) 特性實作私有變數的 get 與 set 存取子
/*
請依據以下範本撰寫一個 MyFunc 函式:
function MyFunc() {
// YOUR CODE HERE
}
撰寫完成後,必須可以正常執行以下程式碼:
var o = MyFunc();
o.get_a() // 回傳 undefined
@kephin
kephin / exercise_after_class_2.js
Last active July 17, 2017 02:25
課後練習題
/*
請完成以下 API 設計,讓你的程式可以執行以下程式碼,並且得到預期的結果。串接 add 的次數沒有上限,最後一個 API 是 result() 才會回傳結果。
$(1).add(2).add(3).add(99).result() === 105
$(1).add(2).add(3).result() === 6
*/
function $(baseNumber) {
let total = baseNumber;
const obj = {};
obj.add = function(number) {
/*
請完成以下 function a() {} 函式的程式撰寫,讓你的程式可以執行以下程式碼,並且得到預期的結果。
※ 傳入數字的範圍只有 0 ~ 9,且回傳型別必須為 number
a(2)[0](3).run() === 203
a(2)[3](3).run() === 233
a(5)[8](2).run() === 582
a(9)[9](9).run() === 999
*/
function a(first) {
@kephin
kephin / answer1.md
Last active July 31, 2019 09:47
Return True if the 2nd array is a subset of 1st array, or False if not

Question 1

Write a function that takes two arrays as input, each array contains a list of A-Z; Your program should return True if the 2nd array is a subset of 1st array, or False if not.

For example:

  • isSubset([A,B,C,D,E], [A,E,D]) = true
  • isSubset([A,B,C,D,E], [A,D,Z]) = false
  • isSubset([A,D,E], [A,A,D,E]) = true
@kephin
kephin / answer2.md
Last active August 15, 2017 14:56
Computational complexity of the answer in Question 1

Question 2

What is the computational complexity of your answer in Question 1? Can you explain why?

  • O(n^2)
  • O(nlogn)
  • O(n)
  • O(logn)

Answer:

@kephin
kephin / answer3.md
Last active August 15, 2017 14:42
Retrun a list of the next fibonacci number

Question 3

Write a function that takes an array of integers as input. For each integer, output the next fibonacci number.

Fibonacci number of Fn is defined by:

Fn = Fn-1 + Fn-2

F1 = 1, F2 = 1

@kephin
kephin / answer4.md
Last active August 15, 2017 15:22
Explain what is the bug in the following JavaScript function, and how to correct it.

Question 4

Please explain what is the bug in the following Javascript function, and how to correct it.

function createArrayOfFunctions(y) {  
  var arr = [];  
  for (var i = 0; i < y; i++) {   
    arr[i] = function(x) {
      return x + i;
@kephin
kephin / tutorial.md
Created June 19, 2018 05:52 — forked from swalkinshaw/tutorial.md
Designing a GraphQL API

Tutorial: Designing a GraphQL API

This tutorial was created by Shopify for internal purposes. We've created a public version of it since we think it's useful to anyone creating a GraphQL API.

It's based on lessons learned from creating and evolving production schemas at Shopify over almost 3 years. The tutorial has evolved and will continue to change in the future so nothing is set in stone.

// Start a method chain that will throw an error:
// --> foo() [catches error]
// -----> bar() [catches / rethrows error]
// --------> baz() [throws error]
foo();
function foo() {
try {
bar();