Skip to content

Instantly share code, notes, and snippets.

View slopeofhope81's full-sized avatar

Steve Lim slopeofhope81

View GitHub Profile
@slopeofhope81
slopeofhope81 / gist:8825339
Created February 5, 2014 14:53
Write a function extractDate that takes such a paragraph as its argument, extracts the date, and returns it as a date object.
//"died 27/04/2006: Black Leclère"
var p = "died 27/04/2006: Black Leclère"
function extractDate(p) {
function find(start, length) {
return p.slice(start, start + length)
}
return new Date(find(11, 4), find(8, 2) - 1, find(5, 2))
}
@slopeofhope81
slopeofhope81 / gist:8826558
Created February 5, 2014 15:49
Write a function countZeroes, which takes an array of numbers as its argument and returns the amount of zeroes that occur in it. Use reduce
Make the function abstract using reduce.
function countZeroes(numbers){
function count(total,num){
return total+(num ===0?1:0); <<===this is ternary equation!!!
}
return reduce(count, 0, numbers)
};
@slopeofhope81
slopeofhope81 / gist:8826927
Created February 5, 2014 16:04
write a map and forEach function
write a map and forEach function:
function forEach(array, action){
for (var i =0; i < array.length; i++){
action(array[i]));
}
};
function map(func,array){
var result =[];
@slopeofhope81
slopeofhope81 / gist:8845219
Last active August 29, 2015 13:56
defineProperty is JS
To set attributes of a property or to create a new property with the specified attributes.
var a = {};
Object.defineProperty(0, "x", {value:1, writable: true, enumerable:false, configurable: true});
Guess what this does?
Also changing this (data property to an accessor property) =>
Object.defineProperty(0, "x", { get: function(){ return 0} });
@slopeofhope81
slopeofhope81 / gist:8968066
Last active August 29, 2015 13:56
jquery button interaction=DOM manipulation(it makes change to the DOM's representation of the page in the browser.
$(document).ready(function(){
$("#move_up").click(function(){
$("#change_me").animate({top:100}, 1000);
});
$("#move_down").click(function(){
$("#change_me").animate({top:400},1000)
});
$("#change_color").click(function(){
$("#change_me").css("background-color","purple");
});
@slopeofhope81
slopeofhope81 / gist:8976024
Created February 13, 2014 14:32
jquery DOM manipulation #2
$(document).ready(function () {
$(".section").click(checkForCode);
function getRandom(n) {
return Math.floor(Math.random() * n);
}
var hideCode = function () {
var num = getRandom(4);
$(".section").each(function (index, value) {
if (num == index) {
@slopeofhope81
slopeofhope81 / gist:9057028
Created February 17, 2014 19:14
Making a quiz questions #1
var allQuestions = [];
allQuestions[0]={
question: "what is your name?",
choices:["steve", "kevin", "chloe","haeseong"],
correctAnswer: 1
};
allQuestions[1]={
question: "what is your passion?",
choices:["programmer", "teacher", "scientist","home builder"],
correctAnswer: 1
@slopeofhope81
slopeofhope81 / gist:9072649
Created February 18, 2014 15:03
Now I understand the power of bind method how it will always run in the given context.
For example, take a look at the first example below:
1)(function(){
var cart={
item: "product1",
price: 35.50,
quantity: 2,
buying: function(){
alert("you bought"+this.item);
}
};
@slopeofhope81
slopeofhope81 / gist:9093298
Created February 19, 2014 14:35
I made this quiz app that has following: 1)radio button choices 2)it will show score upon completion 3)can show any # of questions and choices4)tally the user's score and remove everything else in the form
<html>
<head><!--html-->
<title>example1</title>
<link rel="stylesheet" type="text/css" href="do.css">
</head>
<body>
<div id="point"></div>
<div id="question"></div>
<form name="radioAnswers">
@slopeofhope81
slopeofhope81 / gist:9113201
Created February 20, 2014 13:11
my latest endeavor with javascript
function loadasync(url){
var head=document.getElementsByTagName("head")[0];
var script=document.createElement("script");
script.src=url;
head.appendChild(script);
}
function urlArg(){
var args = {};
var query = window.location.search.substring(1);