Skip to content

Instantly share code, notes, and snippets.

View shaggybb's full-sized avatar

Ivan Rubio shaggybb

  • Xing
  • Barcelona, Spain
View GitHub Profile
@shaggybb
shaggybb / avoidDuplicates.js
Last active August 29, 2015 14:15
Avoid duplicates in JavaScript
var a = [1,2,3,4,5,6,7,7,7,7,7,8];
a = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
});
@shaggybb
shaggybb / spliceArray.js
Last active August 29, 2015 14:15
Add array inside another one in specific position
if (!Array.prototype.spliceArray) {
Array.prototype.spliceArray = function(index, n, array) {
return Array.prototype.splice.apply(this, [index, n].concat(array));
}
}
@shaggybb
shaggybb / wordsCounter.js
Created February 11, 2015 14:11
Words counter with reduce
var text ="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
var counter = text.split(" ").reduce(function(total, item){
total[item] = total.hasOwnProperty(item) ? ++total[item] : 1;
return total;
}, {});
console.log(counter);
@shaggybb
shaggybb / unhandled-exceptions.js
Created January 28, 2018 16:18 — forked from ericelliott/unhandled-exceptions.js
Capturing Unhandled Exceptions
window.onerror = function(message, file, line, column, error) {
error = error || {};
$.ajax({
method: 'POST',
url: 'https://yourapp.com/path/to/error/log',
data: JSON.stringify({
message: message,
file: file,
line: line,
column: column,
@shaggybb
shaggybb / 1.scopes.js
Created January 24, 2020 08:59 — forked from marciobarrios/1.scopes.js
Practical frontend interview
(function() {
var a = b = 5;
})();
console.log(b);
// 1. What will be printed on the console?
// 2. Rewrite the code to return the same result but with the variable declarations separated
// 3. Enable strict mode to explicitly reference the scope