Skip to content

Instantly share code, notes, and snippets.

@swaraj89
Created May 12, 2017 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swaraj89/d742e4c1c3f7d675c9f1c9773751c034 to your computer and use it in GitHub Desktop.
Save swaraj89/d742e4c1c3f7d675c9f1c9773751c034 to your computer and use it in GitHub Desktop.
Solutions to BLITZ problem statements
/**
*Simpler solution using for loop; without using any array.indexOf()
*/
Array.prototype.getIndicesOf = function(item){
var indices = [];
for(var i=0; i < this.length; i++){
if(this[i] === item){
indices.push(i);
}
}
return indices;
};
function userMap(callback){
var THIS, Arr, index;
if(!this){
throw new TypeError('This is null or not defined');
}
var oThis = Object(this);
var len = oThis.length >>> 0;
if(typeof callback !== 'function'){
throw new TypeError(callback +' is not a function');
}
if(arguments.length > 1){
THIS = arguments[1];
}
Arr = new Array(len);
index=0;
while (index < len) {
var currValue, mappedValue;
if (index in oThis) {
currValue = oThis[index];
mappedValue = callback.call(THIS, currValue, index, oThis);
Arr[index] = mappedValue;
}
index++;
}
return Arr;
}
if(!Array.prototype.map){
Array.prototype.userMap = userMap;
}
function getSum(num1){
if(arguments.length > 1){
var sumOfNums = 0;
for(var i=0; i<arguments.length; i++){
sumOfNums+=arguments[i];
}
return sumOfNums;
}else{
return function(num2){
return num1+num2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment