Skip to content

Instantly share code, notes, and snippets.

@rajeebbanstola
Last active March 3, 2018 06:01
Show Gist options
  • Save rajeebbanstola/d1f5a62747ef8a8ae747164c756a0a84 to your computer and use it in GitHub Desktop.
Save rajeebbanstola/d1f5a62747ef8a8ae747164c756a0a84 to your computer and use it in GitHub Desktop.
function sumAll(arr) {
arr.sort(function(a,b) { return a - b } );
num1 = arr[0];
num2 = arr[1];
numbers = [];
for(i = num1; i <= num2; i++){
numbers.push(i);
}
total = 0;
numbers.map(function(value){
total = total + value;
});
return total;
}
sumAll([5, 10]);
function diffArray(arr1, arr2) {
return arr1.filter(function(elem){
return arr2.indexOf(elem) < 0;
}).concat(arr2.filter(function(elem){
return arr1.indexOf(elem) < 0;
}));
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
function convertToRoman(input) {
var numbers = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
var romans = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
var returnStr = "";
numbers.map(function(number, i) {
while (input >= number) {
returnStr += romans[i];
input -= number;
}
});
return returnStr;
}
convertToRoman(36);
// This must be the shittest solution anyone could ever come up with, lol. It works though
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
var key = Object.keys(source);
// Only change code below this line
collection.forEach(function(element, currentEl) {
for(i=0; i < key.length; i++){
if(element.hasOwnProperty(key[i]) && element[key[i]] == source[key[i]] && element[key[i+1]] == source[key[i+1]] ){
if( arr.indexOf(collection[currentEl]) === -1 ){
arr.push(collection[currentEl]);
}
}
}
});
// Only change code above this line
return arr;
}
whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });
function myReplace(str, before, after) {
afterArray = after.split("");
if( before.charAt(0) == before.charAt(0).toUpperCase() ) {
afterArray[0] = after.charAt(0).toUpperCase();
}
after = afterArray.join("");
return str.replace( new RegExp( before, 'g' ), after );
}
myReplace("His name is Tom", "Tom", "john");
function translatePigLatin(str) {
var vowels = ["a", "e", "i", "o", "u"];
var regex = /[aeiou]/gi;
// Check if the first character is a vowel
if (str[0].match(regex)) {
finalStr = str + 'way';
} else {
vowelToBeRemoved = str.indexOf(str.match(regex)[0]);
finalStr = str.substr(vowelToBeRemoved) + str.substr(0, vowelToBeRemoved) + 'ay';
}
return finalStr;
}
translatePigLatin("california");
function pairElement(str) {
splitted = str.split("");
array = [];
splitted.forEach(function(element){
switch(element){
case 'G':
array.push(['G', 'C']);
break;
case 'C':
array.push(['C', 'G']);
break;
case 'A':
array.push(['A', 'T']);
break;
case 'T':
array.push(['T', 'A']);
break;
}
});
return array;
}
pairElement("GCG");
function fearNotLetter(str) {
for(var i = 0; i < str.length; i++) {
/* code of current character */
var code = str.charCodeAt(i);
/* if code of current character is not equal to first character + no of iteration
hence character has been escaped */
if (code !== str.charCodeAt(0) + i) {
/* if current character has escaped one character find previous char and return */
return String.fromCharCode(code - 1);
}
}
return undefined;
}
// test here
fearNotLetter("abce");
function booWho(bool) {
return typeof bool === 'boolean';
}
booWho(null);
function uniteUnique(arr) {
var args = Array.from(arguments);
var finalArray = [];
for(i=0; i < args.length; i++){
for (var j = 0; j < args[i].length; j++) {
if (!finalArray.includes(args[i][j])) {
finalArray.push(args[i][j]);
}
}
}
return finalArray;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
function convertHTML(str) {
//Chaining of replace method with different arguments
str = str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,"&apos;");
return str;
}
convertHTML("Dolce & Gabbana");
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
return str.replace(/ /g,'-').replace(/_/g,'-').toLowerCase();
}
spinalCase('This Is Spinal Tap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment