Skip to content

Instantly share code, notes, and snippets.

@PhilippeRoy
Created September 4, 2016 08:18
Show Gist options
  • Save PhilippeRoy/6793028396d0e2d219897834318af6fd to your computer and use it in GitHub Desktop.
Save PhilippeRoy/6793028396d0e2d219897834318af6fd to your computer and use it in GitHub Desktop.
Coding Challenge for CRG
// Write a program which gets a users timeline from Twitter and then prints out each odd item.
// Node.js
// Functional programming - No Loop - return a new array of odd values
function returnOdd(arr) {
return arr.filter(function(el, index){
return index % 2 ? el : null;
})
}
// Add dependencies
var express = require('express');
var Twitter = require('twitter');
// Create express server
var server = express();
var client = new Twitter({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
access_token_key: TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
});
server.get('/', function(req, res) {
var params = {screen_name: 'screen_name'};
// get user_timeline
client.get('statuses/user_timeline', params, function(error, tweets, response) {
if (!error) {
// Print to console
console.log(returnOdd(tweets));
// Send Response
res.send(returnOdd(tweets))
}
});
});
// Start server
server.listen(3000, function () {
console.log('Server listening on port 3000!');
});
// Write a program which makes 4 AJAX calls in sequential order, one after the other, but never at the same time.
// ============================================================================
// Vanilla
// Wrapper for an ajax request with an optional callback + closure for counter
function ajax(callback){
var counter = 0;
var url = "path/to/resource/";
var method = "GET";
return function(){
var r = new XMLHttpRequest();
r.open(method, url , true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
console.log("Success: " + (++counter) + ' : ' + r.responseText);
if (typeof callback === 'function'){ callback(); }
};
r.send();
}
}
// Make an array of requests
var request = [];
var howManyRequests = 4;
var a = ajax();
for ( var i = 0; i < howManyRequests; i++){
request[i] = a
}
// Callback Hell
request[0](
request[1](
request[2](
request[3]()
)
)
)
// ============================================================================
// Jquery + Promises + Recursion
function ajax(url, depth, howManyLeft){
$.ajax(url)
.done(function(){
console.log("Success: " + (++depth))
howManyLeft--;
if(howManyLeft > 0){
ajax(url, depth, howManyLeft);
}
}
);
}
ajax('path/to/resource/', 0, 4 );
// Create a class system to represent a family hierarchy.
// JavaScript is an object-based language based on prototypes, rather than being class-based.
// Prototypal inheritance
// Animal Constructor
function Animal(type){
this.type = type || '';
}
// Cat Constructor
// Cat inherits from Animal
function Cat(){
Animal.call(this,'cat');
}
Cat.prototype = new Animal;
// Share sound amonst instances of Cat
Cat.prototype.sound = function(){
console.log('Meow!');
}
// Dog Constructor
// Dog inherits from Animal
function Dog(){
Animal.call(this,'dog');
}
Dog.prototype = new Animal;
// Share sound amonst instances of Dog
Dog.prototype.sound = function(){
console.log('Woof!');
}
// Fish Constructor
// Fish inherits from Animal
function Fish(){
Animal.call(this,'fish');
}
Fish.prototype = new Animal;
// Add new method that is shared across all instances that inherit from animal
Animal.prototype.whatType = function(){
console.log(this.type);
}
// Creat instances
var garfield = new Cat();
garfield.sound(); // Meow!
var simon = new Cat();
simon.sound(); // Meow!
var snoopy = new Dog();
snoopy.sound(); // Woof!
garfield.sound(); // Meow!
var nemo = new Fish();
// nemo.sound(); // Won't work becasue the sound method does not exsist in prototypal chain for Fish
// Method + Properties inherited from Animal
garfield.whatType(); // cat
simon.whatType(); // cat
snoopy.whatType(); // dog
nemo.whatType(); // fish
// Write a program which can find and display all occurrences of string that
// matches a standard Australian phone number in the following format: (xx) (xxxx) (xxxx)
// Regex
function findPhoneNumber(text) {
text = text.toString();
// Regex - (xx) (xxxx) (xxxx)
var myRe = /(\d{2})(\s{1})(\d{4})(\s{1})(\d{4})/gm;
// Loop through and display found matches
var match = [];
var matches = [];
while ((match = myRe.exec(text)) !== null) {
var msg = 'Found ' + match[0] + '. ';
// Display results
matches.push(match[0]);
console.log(msg);
}
console.log(matches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment