Skip to content

Instantly share code, notes, and snippets.

@codingwithrachel
codingwithrachel / recursion.js
Last active November 30, 2018 15:27
Recursion
//reverse string
function solution(str){
if(str === "") return "";
return solution(str.substr(1,str.length-1)) + str[0];
}
//fibonacci numbers
function fibonacci(num) {
@codingwithrachel
codingwithrachel / while.js
Last active November 30, 2018 15:27
while loop
//spiral
var spiral = function(n){
var direction;
x=0
y=0
resultArray = [[0,0]]
for (var k=1; k < n; k++){
direction = k%2;
@codingwithrachel
codingwithrachel / prototype.js
Last active February 29, 2016 21:55
prototype
//All, none, any
Array.prototype.all = function (p) {
return this.length > 0 && this.filter(p).length === this.length;
};
Array.prototype.none = function (p) {
return this.filter(p).length === 0;
};
@codingwithrachel
codingwithrachel / codewars.js
Last active November 30, 2018 15:26
codewars
//find count of most frequent item in array
function mostFrequentItemCount(store) {
var frequency = {}
var max = 0;
var result;
for (var v in store) {
frequency[store[v]] = (frequency[store[v]] || 0)+1;
if(frequency[store[v]] > max) {
max = frequency[store[v]];
// result = store[v];
@codingwithrachel
codingwithrachel / regex.js
Last active November 30, 2018 15:26
regex
//detect pangram (has all alphabet characters)
function isPangram(string){
var alphabets = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
var a = alphabets.split(",")
string = string.replace(/[^a-zA-Z0-9]+/g,"").toLowerCase()
for(var i=0;i<a.length;i++) {
if(string.indexOf(a[i]) === -1) return false;
}
return true;
}
@codingwithrachel
codingwithrachel / count.js
Created September 5, 2018 15:17
Count number of words in a string in what line
function count(file) {
var lines = file.split("\n")
var wordToCounterMap = {}
var wordToLineMap = {}
for(var k = 0; k < lines.length; k++) {
countWords(lines[k].split(" "), k)
}
function countWords(words, lineNumber) {
@codingwithrachel
codingwithrachel / mergeIntervals.js
Created September 5, 2018 16:32
Merge meeting intervals
function mergeIntervals(intervals) {
if(intervals.length < 2) return intervals
intervals = intervals.sort(function(a,b) {
return a[0] - b[0]
})
var stack = []
stack.push(intervals[0])
var top
for(var i = 1; i < intervals.length; i++) {
top = stack[stack.length - 1]
@codingwithrachel
codingwithrachel / anagram.js
Created September 25, 2018 14:49
create anagrams
function anagram(str) {
var obj = {}
function recurse(combo, feed) {
if(!feed.length) {
obj[combo] = 1
return
} else {
for(var i=0; i < feed.length; i++) {
recurse(combo+feed[i], feed.slice(0,i) + feed.slice(i+1))
}
@codingwithrachel
codingwithrachel / rps.js
Created September 25, 2018 15:07
create combinations of rock paper scissors
function rps(rounds) {
var obj = {}
var feed = ['r','p','s'];
function recurse(combo, numRoundsLeft) {
if(!numRoundsLeft) {
obj[combo] = 1
return
}
@codingwithrachel
codingwithrachel / perform_raindrop.js
Last active December 7, 2018 21:44
Authenticating with Hydro Raindrop Contract via web3
async function sendSignedTransaction(web3, privateKey, nonce, gasPrice, gasLimit, to, from, data) {
try {
const rawTx1 = {
nonce: nonce,
gasPrice: gasPrice,
gasLimit: gasLimit,
to: to,
from: from,
data: data