Created
May 12, 2017 05:09
-
-
Save swaraj89/d742e4c1c3f7d675c9f1c9773751c034 to your computer and use it in GitHub Desktop.
Solutions to BLITZ problem statements
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
/** | |
*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; | |
}; |
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
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; | |
} |
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
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