Skip to content

Instantly share code, notes, and snippets.

View kselax's full-sized avatar

Kselax kselax

View GitHub Profile
@kselax
kselax / exercises1.js
Last active November 19, 2018 21:55
chapter 4, exercises
// The sum of a range
// The introduction of this book alluded to the following as a nice way to compute
// the sum of a range of numbers:
// console.log(sum(range(1, 10)));
// Write a range function that takes two arguments, start and end , and returns
// an array containing all the numbers from start up to (and including) end .
// Next, write a sum function that takes an array of numbers and returns the
// sum of these numbers. Run the example program and see whether it does
// indeed return 55.
// As a bonus assignment, modify your range function to take an optional third
@kselax
kselax / index1.js
Last active November 15, 2018 01:34
chapter 3 functions
// Minimum
// The previous chapter introduced the standard function Math.min that returns
// its smallest argument. We can build something like that now. Write a function
// min that takes two arguments and returns their minimum.
let minimum = (a, b) => {
if (a < b) {
return a
}
return b
@kselax
kselax / index.js
Created November 15, 2018 00:16
recursion
function findSolution(target) {
function find(current, history) {
if (current == target) {
return history;
} else if (current > target) {
return null
} else {
return find(current + 5, `(${history} + 5)`) ||
find(current * 3, `(${history} * 3`)
}
@kselax
kselax / index.js
Created November 14, 2018 23:11
priority let by var
{
var x = 1
{
let x = 2 // SyntaxError: Identifier 'x' has already been declared
{
function f(){
let x = 10
{
var x = 20 // SyntaxError: Identifier 'x' has already been declared
{
@kselax
kselax / index.js
Created November 14, 2018 23:07
let scope variable can't be break by var
{
let x = 1
{
var x = 2 // SyntaxError: Identifier 'x' has already been declared
{
console.log('scope1 x = ', x);
}
console.log('scope2 x = ', x);
}
console.log('scope3 x = ', x);
@kselax
kselax / index.js
Created November 14, 2018 22:46
let scope
{
var x = 11
{
let x = 10
{
console.log('x (unknown) = ', x); // can you guess which x here?
function f() {
var a = 1
{
var a = 2
@kselax
kselax / index.js
Last active November 14, 2018 22:42
the two scope of javaScript
{
var x = 11
{
var x = 10
{
function f() {
var a = 1
{
var a = 2
{
@kselax
kselax / index.js
Created November 14, 2018 22:26
x is seeing anywhere
{
var x = 11
{
var x = 10
{
var x = 30;
}
console.log('x = ', x); // 30
}
console.log('x = ', x); // 30
@kselax
kselax / index.js
Created November 14, 2018 11:45
MemDbKeyStore
class MemDbKeyStore {
constructor(name, store, initFn) {
this.name = name
this.store = store
this.store[name] = {}
if (typeof initFn === 'function') {
initFn.call(this)
}
}
@kselax
kselax / index.js
Last active November 14, 2018 11:20
class DB
'use strict'
const fs = require('fs')
const Table = require('./Table') // return class Table
const Log = require('../../model/Log') // return call Log
class DB {
constructor() {
this.pool = null
this.tablesNameArray = []