Skip to content

Instantly share code, notes, and snippets.

@dladowitz
Created July 7, 2012 05:12
Show Gist options
  • Save dladowitz/3064848 to your computer and use it in GitHub Desktop.
Save dladowitz/3064848 to your computer and use it in GitHub Desktop.
Javascript Assesement 1
// David Ladowitz
// start time: 8:00pm
// Exercise 1....................................................
var newConcat = function(first, last){
return (first + last)
};
newConcat('david', 'ladowitz')
// Exercise 2....................................................
var sum = function(array){
var results = 0;
for(i in array){
results = results + array[i];
}
return results;
};
// Exercise 3....................................................
["javascript", "is", "a", "nice", "programming", "language", "as", "well"]
// What value is stored at index 3? -- 'nice'
// What is the index of the word "javascript?" -- 0
// What is the length of the array? -- 8
// Exercise 4....................................................
String.prototype.funString = function(){
var results = [];
for(i in this){
if(i % 2 === 1){
results.push(this.charAt(i).toUpperCase());
}
else{
results.push(this.charAt(i));
}
}
return results.reverse().join('');
};
// Exercise 5....................................................
var fizzblam = function(){
for(var i = 1; i < 101 ; i++){
if(i % 7 === 0 && i % 5 === 0){
console.log("FizzBlam");
}
else if(i % 5 === 0){
console.log("Fizz");
}
else if(i % 7 === 0){
console.log("Blam");
}
else{
console.log(i);
}
}
};
// Exercise 6....................................................
var GuessingGame = function(answer){
return {
guess: function(guess){
if(guess === answer){
this.solved = true;
return "correct"
}
else if(guess < answer){
return "low"
}
else{
return "hi"
}
},
solved: false
}
};
// Usage printout from console:
//
// game1 = GuessingGame(10)
// Object
// game1.solved
// false
// game1.guess(5)
// "low"
// game1.guess(15)
// "hi"
// game1.guess(10)
// "correct"
// game1.solved
// true
// Exercise 7....................................................
// var myVariable = 'value'
// Here we are setting myVariable to be a local variable with the value of 'value'.
// It uses var to make it a local variable and not a global variable.
// this.myVariable = 'value'
// I'm not entirely sure what is happening here. Generally 'this' can be used like 'self' in ruby.
//
// Exercise 8....................................................
// Example 1: This iterates through each integer in the array, multiplies the integer by 100
// and prints it to the console. The original array is unchanged.
// Example 2: This iterates through each integer in the array, and returns the largest integer in the array
// It does this by assigning the first integer in the array to 'foo', then compares the next integer to 'foo'.
// If this integer is larger it will assign it to foo and continue for all integers. The original array is unchanged.
// Example 3: This returns the an array with the original integers increased by 80. It does this by iterating through
// each integer, adding 80 to it, then pushing the increased number to a new array. When its done it returns this
// new array. The original array is unchanged.
// Example 4: This returns an array of only the even numbers from the given array. It does this by iterating through
// the original array and tests the result of the interger % 2. All even numbers evaluate to 0. The even numbers are
// then put into a new array. The new array is returned. The original array is unchanged.
// Exercise 9....................................................
// fruits().peaches;
// -- 7
// fruits().peaches();
// -- Error
// 'peaches' is being called as a function with (). It is only a property though.
// fruits().apples;
// -- Undefined
// 'apples' is defined as a local variable in the function. It is not available outside the function.
// oranges;
// -- Error
// 'oranges' is being called as if it was it's own variable or function. It is only defined as a local variable inside of the fruits function.
// Even if it was called properly it still wouldn't be available outside the fruits function.
// fruits().crate();
// -- 10
// Exercise 10....................................................
// console.log(animals.dog);
// -- 'woof'
// This is called properly
// console.log(animals.stepOnTail);
// -- meowZERS!!
// This should returns cat +"ZERS!!". cat is already defined as "meow", so the result is 'meowZERS!!'
// console.log(animals.cat);
// -- undefined
// This is because cat is not a property of animals, but is being called as if it was a properly of animals.
// Calling cat on it's own, would return 'meow'
// console.log(animals.duck);
// -- function () {
// return 'quack';
// }
// This is because we are calling the properly of duck, which happens to be a function. We didn't include
// (), which would call the function associated with duck,
// console.log(animals.duck());
// -- quack
// Here we call the function associated with duck, which retuns 'quack'.
// Exercise 11....................................................
// Return:
// This gives back a result to whatever may have called the function that is now giving the result. Generally
// this is a function or method and used by the computer, not a human. In Javascript you must explicity declare "return" if you want a function to
// return something. This is different than ruby, which returns the the last evaluated line of code.
// In the example below foo is returned to whatever called functionTwo. Without the 'return' keyword nothing would
// cbe given back.
// var functionTwo = function(array) {
// var foo = array[0];
//
// for (var i in array) {
// if (array[i] > foo) {
// foo = array[i];
// }
// }
// return foo;
// };
// Console.log:
// This prints the results out to the Javascript console. Generally this console is part of a web browser.
// It is similir to ruby's 'print' or 'puts' functions. It will not give the results back to any other function.
// It is generally for human usage.
//
// In the example below a bunch of integers are printed on the console(screen) for humans to view.
// var functionOne = function(array) {
// for (var i in array) {
// console.log(array[i] * 100);
// }
// };
// Exercise 12....................................................
// I had to look up the equivalet of Time.now in JS. It's 'new Date()'.
// fibSlow(35) took around 300 miliseconds, it seemed to change on different runs.
// fibFast(35) took around 1 miliseconds, it seemed to change on different runs.
//fibSlow takes so much longer because it uses recursion. For each number over 1 it needs to run the entire
// function all over again. Each run takes time.
// fibFast does not use recursion and only uses a loop. The loop is much faster than running the enitre function
// multiple times.
var fibSlow = function(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
return fibSlow(n-1) + fibSlow(n-2);
}
};
var time1 = new Date();
fibSlow(35)
var time2 = new Date();
console.log(time2 - time1);
var fibFast = function(n) {
var time3 = new Date();
console.log(time3);
fib = [0,1];
for (var i = 2; i <= n; i++) {
var last = fib[1];
var fibN = fib[1] + fib[0];
var fib = [last, fibN];
}
console.log(fib[1]);
var time4 = new Date();
console.log(time3);
console.log(time4 - time3);
};
// Last Question....................................................
// end time: 9:54pm
// This test took just under 2 hrs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment