Skip to content

Instantly share code, notes, and snippets.

@utumno86
Last active August 29, 2015 14:21
Show Gist options
  • Save utumno86/b797b0a8dfabd7c62259 to your computer and use it in GitHub Desktop.
Save utumno86/b797b0a8dfabd7c62259 to your computer and use it in GitHub Desktop.
javascriptFunctions
for (i=1; i<=100; i++){
if (i % 3 == 0 && i % 5 == 0){
console.log( "Fizzbuzz!");
}
else if (i % 3 == 0 && i % 5 != 0) {
console.log("Fizz");
}
else if (i % 3 != 0 && i % 5 == 0){
console.log("Buzz");
}
else{
console.log(i);
}
}
(1..100).each do |i|
if i % 3 == 0 && i % 5 == 0
puts "Fizzbuzz!"
elsif i % 3 == 0 && i % 5 != 0
puts "Fizz"
elsif i % 3 != 0 && i % 5 == 0
puts "Buzz"
else
puts i
end
end
// 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 stringArray = ["Bob", "Tom", "George", "Alfred"];
var sortedStringArray = stringArray.sort();
console.assert(stringArray[3] === "Tom");
// .concat()
var newArray = sortedStringArray.concat(stringArray);
console.assert(newArray[4] === "Alfred");
// .indexOf()
indexNumber = newArray.indexOf("Bob");
console.assert(indexNumber === 1);
// .split()
var newString = newArray[0].split("f");
console.assert(newString[0] === "Al");
// .join()
var joinedArray = newArray.join(",");
console.assert(joinedArray === "Alfred,Bob,George,Tom,Alfred,Bob,George,Tom");
// .pop()
var poppedArray = newArray.pop();
console.assert(poppedArray ==="Tom");
// .push()
var total = newArray.push('Fred', 'Barney');
console.assert (total === 9);
// .slice()
var slicedArray = newArray.slice(0, 5);
console.assert(slicedArray[4] === "Alfred");
// .splice()
var removed = newArray.splice(5, 0, "Norbert");
console.assert(newArray[5] === "Norbert");
// .shift()
var shifted = newArray.shift();
console.assert(newArray[0] === "Bob");
// .unshift()
var unshifted = newArray.unshift("Frank");
console.assert(newArray[0] === "Frank");
// .filter()
var numberedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function isEven(value){
return value % 2 == 0;
};
var filteredArray = numberedArray.filter(isEven);
console.assert(filteredArray[0] === 2);
// .map()
function timesTen(value){
return value * 10;
};
var mappedArray = numberedArray.map(timesTen);
console.assert(mappedArray[1] === 20);
/**
* 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 });
projections = projections.map (function(c) { return { name : c.firstname + " " + c.lastname } });
projections = projections.sort(sortByName);
function sortByName(c1, c2) {
"use strict";
//...
return c1 > c2;
};
console.log(projections);
// 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 stringArray = ["Bob", "Tom", "George", "Alfred"];
var sortedStringArray = stringArray.sort();
console.assert(stringArray[3] === "Tom");
// .concat()
var newArray = sortedStringArray.concat(stringArray);
console.assert(newArray[4] === "Alfred");
// .indexOf()
indexNumber = newArray.indexOf("Bob");
console.assert(indexNumber === 1);
// .split()
var newString = newArray[0].split("f");
console.assert(newString[0] === "Al");
// .join()
// .pop()
// .push()
// .slice()
// .splice()
// .shift()
// .unshift()
// .filter()
// .map()
/**
* 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) {
// YOUR CODE HERE
})
.map(function(c) {
// YOUR CODE HERE
})
.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