Skip to content

Instantly share code, notes, and snippets.

@siddharthray
siddharthray / factorial.html
Last active July 17, 2018 10:46
Factorial of a number
<!doctype html>
<html>
<head>
<script>
function show(){
var i, no, fact;
fact=1;
no=document.getElementById("num").value;
for(i=1; i<=no; i++)
@siddharthray
siddharthray / switchCase.js
Created January 4, 2018 13:19
JavaScript miscellaneous
let calculate = (no1, no2, operation) => {
let result;
switch (operation) {
case 'add':
result = no1 + no2;
break;
case 'subtract':
result = no1 - no2;
break;
}
@siddharthray
siddharthray / prototypes.js
Last active June 16, 2018 17:54
Prototypes
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = getAppleInfo;
}
// anti-pattern! keep reading...
function getAppleInfo() {
return this.color + ' ' + this.type + ' apple';
}
var foo = 10 + '20';
console.log(0.1 + 0.2 == 0.3); // false
console.log((1/10)+(2/10)); // 0.30000000000000004
var add1 = function(x,y) {
return x+y;
}
var add = function(x) {
return function(y) { return x + y; };
}
var addWithTwoArgs = add1(2,5); //7
@siddharthray
siddharthray / promise.js
Last active May 12, 2018 19:06
JavaScript Promise
var calculationPromise = new Promise(function(resolve,reject){
setTimeout(function() {
resolve(1+1);
}, 1000)
});
var calculationPromise2 = new Promise(function(resolve,reject){
setTimeout(function() {
resolve(2+2);
@siddharthray
siddharthray / DOM VS BOM
Last active August 20, 2018 12:23
DOM VS BOM
The BOM (Browser Object Model) consists of the objects navigator, history, screen, location and
document which are children of window.
In the document node is the DOM (Document Object Model),
the document object model, which represents the contents of the page.
You can manipulate it using javascript.
// this is a big array of 76 items I need to split into groups of 10
const hugeArray = Array.from({ length: 76 }, (_, i) => i);
function chunkify(array, chunkSize = 10) {
// make a new array
const chunks = Array.from(
// give it however many slots are needed - in our case 8
// 1-7 with 10 items, and 8th slot will have 6
{ length: Math.ceil(array.length / chunkSize) },
// this is a map function that will fill up our slots