Skip to content

Instantly share code, notes, and snippets.

View slopeofhope81's full-sized avatar

Steve Lim slopeofhope81

View GitHub Profile
@slopeofhope81
slopeofhope81 / gist:8572466
Created January 23, 2014 03:46
Project Euler question #1: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
/*If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*/
var multiples = function (n) {
var sum = 0;
for (var i = 1; i < n; i++) {
if ((i % 3 == 0) || (i % 5 == 0)) {
sum += i;
@slopeofhope81
slopeofhope81 / reverse function
Created January 23, 2014 16:42
How to write a reverse function on an array that has integers
Do you know that the sort method does not work so well on integers in an array since it is used for strings!
That is something I found out today so then how to use a sort method on an array that has integers? Simply add a
custom function to a sort method as an argument like below!
var arr = [1,100,4,30,5,80]
var compare = function(num1,num2){
if (num1 < num2){
return -1;
}
else if (num1 > num2){
return 1;
@slopeofhope81
slopeofhope81 / gist:8596970
Created January 24, 2014 13:13
// Calculate and show the value of 2**10
// Calculate and show the value of 2**10
var total = 1;
var i = 0;
var power = function (n) {
while (i < 10) {
total *= 2;
i++;
};
return total;
@slopeofhope81
slopeofhope81 / gist:8597398
Created January 24, 2014 13:39
Print out ten lines. On the first line there is one '#' character. On the second there are two. And so on.
//Print out ten lines. On the first line there is one '#' character. On the second there are two. And so on.
var string = "#";
var i = 0;
var line =""
while (10 > i) {
line += string
document.write(line+"\n");
i++;
}
@slopeofhope81
slopeofhope81 / gist:8597620
Created January 24, 2014 13:52
//Write a program to ask yourself, using prompt, what the value of 2 + 2 is. If the answer is "4", use alert to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean.
//Write a program to ask yourself, using prompt, what the value of 2 + 2 is. If the answer is "4", use alert to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean.
answer = Number(prompt("What is the value of 2+2?"));
if (answer === 4) {
alert("you are right!");
} else if (Math.abs(4 - answer) > 2) {
alert("you did okay");
} else {
alert("lets go back to kindergarten");
}
@slopeofhope81
slopeofhope81 / eloquent.ex.3.2
Created January 24, 2014 17:04
/*Write a function greaterThan, which takes one argument, a number, and returns a function that represents a test. When this returned function is called with a single number as argument, it returns a boolean: true if the given number is greater than the number that was used to create the test function, and false otherwise.*/
function greaterThan(num) {
return function test(num1) {
if (num > num1) {
return true;
} else {
return false;
}
}
}
@slopeofhope81
slopeofhope81 / eloquent.ex.4.2
Created January 24, 2014 18:01
/*Write a function range that takes one argument, a positive number, and returns an array containing all numbers from 0 up to and including the given number.*/
var arr = [];
function list(n) {
for (var i = 0; i <= n; i++) {
arr[i] = i;
}
document.write(arr);
}
@slopeofhope81
slopeofhope81 / eloquent.ex.4.4
Created January 24, 2014 19:31
/*4.4)Write a function called startsWith that takes two arguments, both strings. It returns true when the first argument starts with the characters in the second argument, and false otherwise. */
function startsWith(a, b) {
if (b.slice(0, a.length) == a) return true;
else return false;
}
a="steve"
b="steve is coding hard"
document.write(startsWith(a,b));
@slopeofhope81
slopeofhope81 / eloquent.email.content
Created January 25, 2014 22:14
my first extensive function/ I need to find a way to refactor...and encapsulate
var mailArchive = retrieveMails();
var livingCats = {"Spot": true};
for (var mail = 0; mail < mailArchive.length; mail++)
var paragraphs = mailArchive[mail].split("\n"); //split the paragraphs
for (var paragraph = 0; paragraph < paragraph.length; paragraph++){
if (startsWith(paragraph, "born")){
var names = catNames(paragraphs[paragraph])
for (var name = 0; name < names.length; names++)
livingCats[names[name]] = true;
@slopeofhope81
slopeofhope81 / eloquent.range
Created January 29, 2014 15:59
//Write a range function that takes two arguments, start and end, //and returns an array containing all the numbers from start up to //(and including) end.
function range(start, end, step) {
var arr = [];
if (arguments[2] == undefined) {
for (var i = start; i <= end; i++)
arr.push(i);
} else {
for (var i = start; i <= end; i += step)
arr.push(i);
}
return arr;