Last active
April 5, 2016 09:25
-
-
Save Drugak/0436f871632879a2dcfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Factorial | |
function factorial(n) { | |
if(n === 0) { | |
return 1; | |
} else { | |
return n * factorial(n-1); | |
} | |
} | |
factorial(5) //= 120 | |
//Closures | |
for (var i = 0; i < 5; i++) { | |
var btn = document.createElement('button'); | |
btn.appendChild(document.createTextNode('Button ' + i)); | |
btn.addEventListener( | |
'click', | |
//--------------- | |
(function(e) { | |
return function() { | |
console.log(e); | |
} | |
})(i) | |
//--------------- | |
); | |
document.body.appendChild(btn); | |
} | |
// If you don't use closures , after click on button you will see always "5" | |
//Clone obj in jacascript | |
var dog = {height: 34,speed: 45}, | |
cat = {old: 45,ololo : 34}; | |
//first way | |
function cloneObj(inbox, outbox){ | |
for (prop in outbox) { | |
inbox[prop] = outbox[prop]; | |
} | |
return inbox | |
} | |
//second way | |
function cloneObj(data1 , data2){ | |
return Object.assign({}, arguments); | |
} | |
//Array Flatten | |
var arr = ["1",["2" ,"3", ["4","5"] , ["6" , ["7","8",["10"]]]]]; | |
function flatten(arr) { | |
var myArr = []; | |
for (var i = 0; i < arr.length; i++){ | |
if (Array.isArray(arr[i])) { | |
myArr = myArr.concat(flatten(arr[i])); | |
} else { | |
myArr.push(arr[i]); | |
} | |
} | |
return myArr; | |
} | |
console.log(flatten(arr)) | |
// Find brackets | |
function findBrackets(str){ | |
var brackets = "{}[]()"; | |
var result = []; | |
var position; | |
for (var i = 0, max = str.split('').length; i < max; i++) { | |
position = brackets.indexOf(str.split('')[i]); | |
if (position > -1) { | |
if (position % 2 === 0) { | |
result.push(position + 1); | |
} else if (result.pop() !== position) { | |
return false; | |
} | |
} | |
} | |
return result.length === 0; | |
} | |
console.log(findBrackets("(2+2)*{fgdfg/dfgh//}{{{}aaa")); | |
console.log(findBrackets("(2+2)*{fgdfg/dfgh//}{}aaa")); | |
console.log(findBrackets("(2+2())*{fgdfg/dfgh//}{}aaa")); | |
console.log(findBrackets("(2+2{)}*{fgdfg/dfgh//}{}aaa")); | |
//Diagonal ddifference | |
//Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals. | |
var a = [ | |
[1,2,3,5], | |
[1,5,9,5], | |
[1,5,0,8], | |
[9,5,0,5] | |
] | |
function diagonalsX() { | |
var result = 0; | |
a.forEach(function(item , i ,a) { | |
result = result + item[i]; | |
}) | |
return result; | |
} | |
function diagonalsY() { | |
var result = 0; | |
a.forEach(function(item , i ,a) { | |
result = result + item[item.length - (i+1)]; | |
}) | |
return result; | |
} | |
console.log(Math.abs(diagonalsX() - diagonalsY())); | |
// Is palindrome | |
var str = 'lool'; | |
[].map.call(str, function(x) { | |
return x; | |
}).reverse().join(''); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
take a look for Object.create() func in spec. At least it will actually clone object instead of copying properties.