Skip to content

Instantly share code, notes, and snippets.

@Harvnlenny
Created May 19, 2015 21:12
Show Gist options
  • Save Harvnlenny/a36e04bfc91c0dc519b3 to your computer and use it in GitHub Desktop.
Save Harvnlenny/a36e04bfc91c0dc519b3 to your computer and use it in GitHub Desktop.
// Part I
/**
* use any of the looping methods discussed in class
*
* 1. for loop,
* 2. Array.forEach,
* 3. custom forEach
*
* to complete the following problems
*/
// 0. write your own forEach() that takes two arguments: an array, and a callback
function forEach(array, callback){
for (var i = 0; i < array.length; i++) {
callback(array[i]);
};
}
// testing your code with console.assert
var total = 1;
forEach([1, 2, 3, 4], function(a){ total *= a; });
// and finally assert; if this fails, the program stops
console.assert(total === 24);
// 1. calculate the sum of numbers (returns the sum (A NUMBER))
function sum(){
// parse arguments into an array
var args = [].slice.call(arguments);
var total = 0;
forEach(args, function(a){ total += a; });
return total;
}
console.assert( sum(1, 2, 3, 4, 5) === 15 )
// 2. calculate the average of numbers (returns the average (A NUMBER))
function average(){
// parse arguments into an array
var args = [].slice.call(arguments);
var total = 0;
forEach(args, function(a){ total += a; });
return total / args.length;
}
console.assert( average(2, 4, 6, 8) === 5 )
// 3. find the largest number of the inputs (returns the largest input (A NUMBER))
function largest(){
// parse arguments into an array
var args = [].slice.call(arguments);
var l = 0;
forEach(args, function(a){
if (a > l) { l = a; };
});
return l;
}
console.assert( largest(2, 4, 6, 8) === 8 )
// 4. find the longest string of the inputs (returns the longest input (A STRING))
function longest(){
// parse arguments into an array
var args = [].slice.call(arguments);
var l = "";
forEach(args, function(a){
if (a.length > l.length) { l = a; };
});
return l;
}
console.assert( longest("shelby", "boss", "mach1", "rousche") === "rousche" )
// 5. write a function that can sort an array of Date objects (returns a NEW ARRAY of Date's)
function sort(){
// parse arguments into an array
var args = [].slice.call(arguments);
return args.sort(function(a,b) {
return a > b;
})
}
var dates = [
new Date("Oct 2, 2015"),
new Date("Oct 1, 2015"),
new Date("Jan 2, 2015"),
new Date("Dec 5, 2014"),
new Date("Mar 27, 2015")
]
var sorted = sort(dates[0], dates[1], dates[2], dates[3], dates[4]);
console.assert(
sorted[0] === dates[3] &&
sorted[1] === dates[2] &&
sorted[2] === dates[4] &&
sorted[3] === dates[1] &&
sorted[4] === dates[0]
)
/**
* PART II
*
* For each of the following Array methods,
*
* 1. use them in an example
* 2. write a console.assert to test them
*/
// .sort()
var grades = [89, 50, 94, 83];
var sorted = grades.sort(function(a, b) { return a - b });
console.assert(
grades[0] === 50 &&
grades[1] === 83 &&
grades[2] === 89 &&
grades[3] === 94
)
// .concat()
var first = "This is the first part ";
var second = "and this would be the second part.";
var phrase = first.concat(second);
//console.log(phrase)
console.assert(
phrase === "This is the first part and this would be the second part."
)
// .indexOf()
var bit = "Tomorrow will be Wednesday.";
var bat = bit.indexOf("w");
//console.log(bat)
console.assert(
bat === 7
)
// .split()
var john = "We are";
var jim = john.split(" ");
//console.log(jim);
console.assert(
jim[0] === 'We' &&
jim[1] === 'are'
)
// .join()
var students = ["Mary", "Larry", "Harry"];
var group = students.join();
//console.log(group);
console.assert(
group === 'Mary,Larry,Harry'
)
// .pop()
var cards = ["Ace", "King", "Queen", "Jack"];
var draw = cards.pop();
//console.log(draw);
console.assert(
draw === "Jack"
)
// .push()
var food = ["breakfast", "lunch"];
var night = food.push("dinner");
//console.log(night);
console.assert(
night === 3
)
// .slice()
var states = ["Georgia", "Florida", "Ohio", "Alaska"];
var south = states.slice(0,2);
//console.log(south);
console.assert(
south[0] === 'Georgia' &&
south[1] === 'Florida'
)
// .splice()
var tv = ["Family Ties", "Seinfeld", "Night Court", "Cheers"];
var eighties = tv.splice(1, 2);
//console.log(eighties);
console.assert(
eighties[0] === "Seinfeld" &&
eighties[1] === "Night Court"
)
// .shift()
var cabinet = ["cereal", "pasta", "snacks",];
var pat = cabinet.shift();
//console.log(pat)
console.assert(
pat === 'cereal'
)
// .unshift()
var hip = [1, 2, 3, 4];
var hap = hip.unshift(5, 6);
//console.log(hap);
console.assert(
hap === 6
)
// .filter()
var yip = [1, 2, 3, 4];
var yap = yip.filter(function(x) { return x < 3 });
//console.log(yap);
console.assert(
yap[0] === 1 &&
yap[1] === 2
)
// .map()
var wer = ["a", "b", "c"];
var war = wer.map(function (x) {return x.toUpperCase()});
//console.log(war);
console.assert(
war[0] === "A" &&
war[1] === "B" &&
war[2] === "C"
)
/**
* PART III
*
* Fill in the sections below marked 'YOUR CODE HERE'.
*
* The code below should find all customers whose first-names start with 'J',
* map() those people into an array of objects that have a name property:
*
* i.e. { name : c.firstname + " " + c.lastname }
*
* then sort them alphabetically
*/
// set up arrays
var numbers = [1, 12, 4, 18, 9, 7, 11, 3, 101, 5, 6];
var strings = ['this', 'is', 'a', 'collection', 'of', 'words'];
var customers = [{
firstname: 'Joe',
lastname: 'Blogs'
}, {
firstname: 'John',
lastname: 'Smith'
}, {
firstname: 'Dave',
lastname: 'Jones'
}, {
firstname: 'Jack',
lastname: 'White'
}];
console.log(customers);
var projections = customers
.filter(function(c) { return c.firstname.indexOf('J') == 0
})
.map(function(c) {return { name : c.firstname + " " + c.lastname
}
})
.sort(sortByName);
function sortByName(c1, c2) {
"use strict";
}
console.log(projections);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment