Skip to content

Instantly share code, notes, and snippets.

@SanthoshRaju91
Created July 19, 2018 09:57
Show Gist options
  • Save SanthoshRaju91/1e2dcce3db1271a2ef2a8fab4fb6e4cd to your computer and use it in GitHub Desktop.
Save SanthoshRaju91/1e2dcce3db1271a2ef2a8fab4fb6e4cd to your computer and use it in GitHub Desktop.
Just practicing
// function Employee(name, dept) {
// this.name = name;
// this.dept = dept;
// }
// Employee.prototype = {
// getName() {
// return this.name;
// },
// getDept() {
// return this.dept
// }
// };
// function Manager(name, dept) {
// Employee.call(this, name, dept);
// this.reports = []
// };
// Manager.prototype = Object.create(Employee.prototype);
// Manager.prototype.constructor = Manager;
// Manager.prototype.addReport = function(name, dept) {
// const emp = new Employee(name, dept);
// this.reports.push(emp);
// }
// Manager.prototype.getReports = function() {
// return this.reports;
// }
// function WorkerBee(name, dept) {
// Employee.call(this, name, dept);
// this.projects = []
// };
// WorkerBee.prototype = Object.create(Employee.prototype);
// WorkerBee.prototype.constructor = WorkerBee;
// WorkerBee.prototype.addProject = function(project) {
// this.projects.push(project);
// }
// WorkerBee.prototype.getProjects = function() {
// return this.projects;
// }
// function SalesPerson(name) {
// WorkerBee.call(this, name, 'sales');
// this.quota = 100;
// }
// SalesPerson.prototype = Object.create(WorkerBee.prototype);
// SalesPerson.prototype.constructor = SalesPerson;
// function Engineer(name) {
// WorkerBee.call(this, name, 'engineering');
// this.machine = "";
// }
// Engineer.prototype = Object.create(WorkerBee.prototype);
// Engineer.prototype.constructor = Engineer;
// Engineer.prototype.addMachine = function(machine) {
// this.machine = machine;
// }
// Engineer.prototype.getMachine = function() {
// return this.machine;
// }
// const chris = new Engineer('Chris Nolan');
// console.log(chris.getDept())
// chris.addMachine('Director');
// console.log(chris.getMachine());
// chris.addProject('Interstellar');
// console.log(chris.getProjects());
// function fibonacci(n) {
// if(n === 0) return 0;
// if(n === 1) return 1;
// return fibonacci(n - 1) + fibonacci(n - 2);
// }
// console.log(fibonacci(12))
// function* fibonacciIt() {
// let fn1 = 0;
// let fn2 = 1;
// while(true) {
// let current = fn1;
// fn1 = fn2;
// fn2 = current + fn1;
// let reset = yield current;
// if(reset) {
// fn1 = 0;
// fn2 = 1;
// }
// }
// }
// let fib = fibonacciIt();
// console.log(fib.next().value);
// console.log(fib.next().value);
// console.log(fib.next().value);
// console.log(fib.next().value);
// console.log(fib.next().value);
// console.log(fib.next().value);
// console.log(fib.next(true).value);
const handler = {
get(target, name) {
return target[name];
},
set(target, name, value) {
target[name] = value;
}
}
const p = new Proxy({}, handler);
p.a = 1;
console.log(p.a)
p.b = 2;
console.log(p.b)
const nums = [3, 2, 1, 100, 4, 200];
nums.sort(function(num1, num2) {
return num1 - num2;
});
console.log(nums)
function createChessBoard() {
const size = 8;
let board = "";
for(let x = 0; x < size; x++) {
for(let y = 0; y < size; y++) {
if((x+y) % 2 === 0) {
board += "#"
} else {
board += " "
}
}
board+="\n"
}
console.log(board);
}
createChessBoard();
function first(word) {
return word[0].toUpperCase();
}
const sentence = ["for", "your", "information"];
const acronym = sentence.map(first);
console.log(acronym.join(''));
Array.matrix = function(rows, cols, initial = 0) {
let arr = [];
for(let i = 0;i <rows; ++i) {
let columns = [];
for(let j=0; j<cols; ++j) {
columns[j] = initial;
}
arr[i] = columns
}
return arr;
}
var matrix = Array.matrix(5, 5, 0);
console.log(matrix);
const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
let total = 0;
let average = 0;
for(let row = 0; row < grades.length; ++row) {
for(let col = 0; col < grades[row].length; ++col) {
total += grades[row][col];
}
average = total / grades[row].length;
console.log('Student ' + parseInt(row+1) + ' got an average : ' + average.toFixed(2));
total = 0;
average = 0;
}
let somesentence = 'Gone with the wind';
let tempArr = somesentence.split(' ');
console.log('Backward ' + tempArr.reverse().join(' '));
function List() {
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
}
List.prototype = {
append(element) {
this.dataStore[this.listSize] = element;
this.listSize++;
},
find(element) {
for(let i=0; i<this.dataStore.length; i++) {
if(this.dataStore[i] === element) {
return i
}
}
return -1;
},
remove(element) {
const foundAt = this.find(element);
if(foundAt > -1) {
this.dataStore.splice(foundAt, 1);
--this.listSize;
return true;
} else {
return false;
}
},
length() {
return this.listSize
},
toString() {
return this.dataStore;
},
insert(element, after) {
const insertPos = this.find(after);
if(insertPos > -1) {
this.dataStore.splice(insertPos + 1, 0, element);
++this.listSize;
return true;
} else {
return false;
}
},
clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = 0;
},
contains(element) {
for(let i=0; i<this.dataStore.length; i++) {
if(this.dataStore[i] === element) {
return true;
}
}
return false;
},
front() {
this.pos = 0
},
end() {
this.pos = this.listSize - 1;
},
prev() {
if(this.pos > 0) {
--this.pos
}
},
next() {
if(this.pos < this.listSize - 1) {
++this.pos;
}
},
currPos() {
return this.pos;
},
moveTo(position) {
this.pos = position;
},
getElement() {
return this.dataStore[this.pos];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment