Skip to content

Instantly share code, notes, and snippets.

View mosdevly's full-sized avatar

Mosdev mosdevly

  • San Francisco, CA
View GitHub Profile
def add (num1, num2)
result = num1 + num2
p "Your result is: #{result}"
end
def subtract (num1, num2)
result = num1 - num2
p "Your result is: #{result}"
end
balance = 0
def deposit(amount)
balance + amount
balance = balance + amount
p balance
end
def withdraw(amount)
balance = balance - amount
@mosdevly
mosdevly / Sort Array
Last active August 29, 2015 14:10
Write a function that takes an array of numbers and sorts them.
// First attempt: http://repl.it/51c
// Bubble sort - Large numbers bubble to the top
function sortArray(arr) {
var tmp;
for (var j = 0; j < arr.length; j++) {
for (var i = 0; i < arr.length-j;i++) {
if (arr[i] > arr[i+1]) {
tmp = arr[i+1];
@mosdevly
mosdevly / isPrime.js
Last active November 26, 2018 07:28
Write a method that takes a number and checks to see if it's prime.
var isPrime = function(number) {
if (number === 1) return false;
for (var i = 2; i <= number/2 ; i++) {
if (number % i === 0) return false;
}
return true;
}
@mosdevly
mosdevly / Index Of
Created November 25, 2014 05:39
Write a method that takes an element of an array and returns it's index.
var indexOf = function (array,element) {
for (var i = 0; i < array.length; i++) {
if (element === array[i]) return i;
}
}
@mosdevly
mosdevly / Flatten Array
Last active August 29, 2015 14:10
Write a method that takes in a nested array and returns a single array containing each element of the former.
var flatten = function (array) {
newArray = [];
for (var idx1 = 0; idx1 < array.length; idx1++) {
if (Array.isArray(array[idx1])) {
for (var idx2 = 0; idx2 < array[idx1].length; idx2++) {
newArray.push(array[idx1][idx2]);
}
} else {
newArray.push(array[idx1]);
@mosdevly
mosdevly / Factorials
Created November 25, 2014 05:33
Write a method that takes an integer and returns the factorial of it.
var factorial = function(num) {
var product = num;
for (var i = 1; i < num; i++) {
product = i * product;
}
return product;
}
@mosdevly
mosdevly / Longest Word
Last active August 29, 2015 14:10
Write a method that takes a string and returns the longest word in it.
var longestWord = function(myString) {
words = myString.split(" ");
longest = "";
for (var word = 0; word < words.length ; word++) {
if (words[word].length > longest.length) {
longest = words[word]
}
}
return longest;
@mosdevly
mosdevly / Sum Nums
Created November 24, 2014 17:42
Write a function that returns the sum of numbers up to a max number given.
var sums = function(num) {
myNum = 0;
for (var i = 0; i <= num; i++) {
myNum += i;
}
console.log(myNum);
}
sums(3);
@mosdevly
mosdevly / Anagrams
Created November 17, 2014 00:07
Write a method that takes two inputs: a single word and a dictionary of other words. The method should output all the words in the dictionary which our single word can scrabble to. For example, cat is an anagram of act.
def word_unscrambler(str, words)
idx = 0
matches = []
while idx < words.length
sorted = words[idx].chars.sort.join
match = str.chars.sort.join
word = words[idx]
if sorted == match