2012年3月3日土曜日

Code Year 2012 - Week 7

今週の課題はループ。
forとwhileだけじゃなく、再帰を使ったループもあった。
再帰は普段使わないからけっこう手間取った。
8週目の課題がこなかったのはもたもたしてたからかな?


// 1. Return of the "return"
var add = function(number1, number2, number3) {
// add up the numbers
var sum = number1 + number2 + number3;
// return the sum!
return sum;
};
// call add twice here, assigning the return values
// to new variables sum1 and sum2
var sum1 = add(1, 2, 3);
var sum2 = add(10, 10, -20);
// 2. Objectionable Objects
var australia = {
weather: "superb",
people: "not many of them but they're all great!",
tourism: "a wonderful place to holiday. please visit!"
};
var jordan = new Object();
jordan.weather = "hot. but so are the people!";
jordan.people = "see above!";
jordan.tourism = "Codecademy's dream team retreat!";
// create your objects here
var unitedStates = {
weather: "of all sorts",
people: "of all types",
tourism: "outstanding place to do business!"
};
var hongKong = new Object();
hongKong.weather = "sometimes quite muggy";
hongKong.people = "was home to House";
hongKong.tourism = "shop til you drop! eat till you pop!";
// 3. The Dot and the Bracket
var australia = {
weather: "superb",
people: "not many of them but they're all great!",
tourism: "a wonderful place to holiday. please visit!"
};
var jordan = new Object();
jordan.weather = "hot. but so are the people!";
jordan.people = "see above!";
jordan.tourism = "Codecademy's dream team retreat!";
// print australia's weather using dot notation
console.log(australia.weather);
// create a new variable "property" and set it to "weather"
var property = "weather";
console.log(jordan[property]);
// now modify "property" to be "people"
property = "people";
console.log(jordan[property]);
// 4. MF! Methods and Functions
var australia = {
hemisphere: "southern",
population: function(newArrivals) {
return 20000000 + newArrivals;
}
};
var australia2 = new Object();
australia2.hemisphere = "southern";
australia2.population = function(newArrivals){
return 20000000 + newArrivals;
};
// create your objects and methods here
var me = {
planet: "Earth",
age: function(yearBorn) {
return 2012 - yearBorn;
}
};
var jeremyLin = new Object();
jeremyLin.team = "Knicks";
jeremyLin.playGame = function(opponent){
return "Tonight the Knicks square off against the " + opponent;
};
// 5. Placeholder: The "this" Keyword
var setName = function(yourName){
this.name = "Your name is " + yourName;
};
// create a human object with the desired method
var human = {
setHumanName: setName
};
// now call human.setHumanName
human.setHumanName("Taro");
// check to see that your method works by printing
// human.name to the console
console.log(human.name);
view raw 1.js hosted with ❤ by GitHub
// 1. Crazy About Classes
// create your class here
function Candy(name){
this.name = name;
}
// this code should print "chocolate" then "gummy bears"
var chocolate = new Candy("chocolate");
console.log( chocolate.name );
var gummyBears = new Candy("gummy bears");
console.log( gummyBears.name );
// 2. Prototyping Fun
// recreate your Candy class here
function Candy(name){
this.name = name;
}
// extend the prototype of Candy with a "printName" method
Candy.prototype.printName = function() {
console.log(this.name);
};
// testing code for your method
var chocolate = new Candy("chocolate");
chocolate.printName();
var gummyBears = new Candy("gummy bears");
gummyBears.printName();
// 3. Sweet Inheritance
// we set up a base class
function Candy() {
this.sweet = true;
}
// create a "Chocolate" class with a "type" argument
function Chocolate(type){
this.type = type;
}
// say that Chocolate inherits from Candy
Chocolate.prototype = new Candy();
// create a "choc" object using the Chocolate constructor
// that has a "type" of "milk"
var choc = new Chocolate("milk");
// print the sweet and type properties of choc
console.log(choc.sweet);
console.log(choc.type);
// 4. Keepin' it Private
function Company(name,yearFounded,profitLevel) {
this.name = name;
this.year = yearFounded;
this.profits = profitLevel;
var bankBalance = 20;
// add your method here
this.getBalance = function() {
return bankBalance - 2;
};
}
var myCompany = new Company("Codecademy", "2011", "insufficient!");
console.log( myCompany.getBalance() );
view raw 2.js hosted with ❤ by GitHub
// 1. One to Five
var i;
for (i = 2; i <= 6; i++) {
console.log(i);
}
// 2. Country by Twos
var i;
for (i = 2; i <= 6; i += 2) {
console.log(i);
}
// 3. Countdown
var i;
for ( i = 5; i > -1; i--) {
console.log(i);
}
view raw 3.js hosted with ❤ by GitHub
// 1. Looping Through Arrays
var i;
var animals = ["cat", "dog", "ferret"];
// Loop goes here
for(i = 0;i < animals.length; i++){
console.log(animals[i]);
}
// 2. Better, Stronger, Faster
var i;
var animals = ["cat", "dog", "ferret"];
var length = animals.length;
// Loop goes here
for(i = 0;i < length; i++){
console.log(animals[i]);
}
// 3. Looping Through Strings
var word = "code";
// Loop goes here
for(var i = 0; i < word.length; i++){
console.log(word[i]);
}
// 4. Building `substring' (Part 1)
// Define substring here
var substring = function(input, start, end){
var i;
for(i = start; i <= end; i++){
console.log(input[i]);
}
};
// Write test here:
// Call substring, passing "lorem ipsum dolor" to `input`, `6` to
// `start`, and `10` to `end`.
substring("lorem ipsum dolor", 6, 10);
// 5. Building `substring' (Part 2)
// Define substring here
var substring = function(input, start, end){
var subset = "";
var i;
for(i = start; i <= end; i++){
subset += input[i];
}
return subset;
};
// Write test here:
// Call substring, passing "lorem ipsum dolor" to `input`, `6` to
// `start`, and `10` to `end`.
console.log(substring("lorem ipsum dolor", 6, 10));
view raw 4.js hosted with ❤ by GitHub
// 1. Conditions!
var condition = true;
while (condition) {
console.log("I only print once!");
condition = false;
}
// 2. Imitation is the Sincerest Form of Flattery
var count = 1;
while(count < 6){
console.log(count);
count++;
}
// 3. Counting Odd Numbers, Again
var count = 1;
while(count < 6){
console.log(count);
count += 2;
}
// 4. Splitting Names (Part 1)
var name = "John Doe";
var getFirstName = function(fullName){
var i = 0;
var length = fullName.length;
while(i < length){
console.log(fullName[i]);
i++;
}
};
getFirstName(name);
// 5. Splitting Names (Part 2)
var name = "John Doe";
var getFirstName = function(fullName){
var i = 0;
var length = fullName.length;
var next = fullName[0];
while(i < length && " " != next){
console.log(fullName[i]);
i++;
next = fullName[i];
}
};
getFirstName(name);
// 6. Splitting Names (Part 3)
var name = "John Doe";
var getFirstName = function(fullName){
var i = 0;
var length = fullName.length;
var next = fullName[0];
var firstName = "";
while(i < length && " " != next){
firstName += fullName[i];
i++;
next = fullName[i];
}
return firstName;
};
console.log(getFirstName(name));
view raw 5.js hosted with ❤ by GitHub
// 1. Counting Up With Recursion (Part 1)
var count = function(start){
if(5 <= start){
console.log(start);
}
};
// 2. Counting Up With Recursion (Part 2)
var count = function(start){
if(5 <= start){
console.log(start);
}else{
console.log(start);
count(start + 1);
}
};
count(1);
// 3. Building `substring` With Recursion (Part 1)
var substring = function(all, start, end){
if(end <= start){
console.log(all[start]);
}
};
// 4. Building `substring` With Recursion (Part 2)
var substring = function(all, start, end){
if(end <= start){
console.log(all[start]);
}else{
console.log(all[start]);
substring(all, start + 1, end);
}
};
substring("lorem ipsum dolor", 6, 10);
// 5. Building `substring` With Recursion (Part 3)
var substring = function(all, start, end){
if(end <= start){
return all[start];
}else{
return all[start] + substring(all, start + 1, end);
}
};
console.log(substring("lorem ipsum dolor", 6, 10));
view raw 6.js hosted with ❤ by GitHub
// 1. Looping Through Objects
var person = {
name: "Morgan Jones",
telephone: "(650) 777 - 7777",
email: "morgan.jones@example.com"
};
for (var propertyName in person) {
// Your code here
console.log(propertyName + ": " + person[propertyName]);
}
// 2. Nesting `for` Loops
for(var i = 0; i < 3; i++){
for(var j = 1; j <= 5; j++){
console.log(j);
}
}
// 3. Printing Out Tables (Part 1)
var table = [
["Person", "Age", "City"],
["Sue", 22, "San Francisco"],
["Joe", 45, "Halifax"]
];
for(var i = 0; i < table.length; i++){
console.log(i);
}
// 4. Printing Out Tables (Part 2)
var table = [
["Person", "Age", "City"],
["Sue", 22, "San Francisco"],
["Joe", 45, "Halifax"]
];
for(var i = 0; i < table.length; i++){
var row = table[i][0];
for(var j = 1; j < table[i].length; j++){
row += " " + table[i][j];
}
console.log(row);
}
// 5. Breaking Out
while (true) {
console.log("I only print once!");
break;
}
view raw 7.js hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿