2012年1月19日木曜日

Code Year 2012 - Week 1

Code Year 2012の1週目。
JavaScriptの入門。変数の定義から、分岐や繰り返しを学んだ。
回答はgistに保存している。自分の勉強の記録のつもり。
まだまだ簡単。



> Manabu
ReferenceError: Manabu is not defined
Oops, try again.
> "Manabu"
==> Manabu
> "Manabu".length
==> 6
> 2+2
==> 4
> 2*2
==> 4
> Manabu
ReferenceError: Manabu is not defined
You've completed this lesson! Start the next one.
>
view raw 1.txt hosted with ❤ by GitHub
>
> confirm("your message here")
==> true
> alert("your message");
You've completed this lesson! Start the next one.
>
view raw 2.txt hosted with ❤ by GitHub
> myName
ReferenceError: myName is not defined
> var myName;
> myName = "Manabu";
==> Manabu
> myName
==> Manabu
> var lastName = "Yamamoto";
> var myFullName = "Manabu Yamamoto";
> lastName.length
==> 8
You've completed this lesson! Start the next one.
>
view raw 3.txt hosted with ❤ by GitHub
> var number = 42;
> number / 2;
==> 21
> number % 10;
==> 2
> var myString = "hello";
> "hello".substring(0, 2);
==> he
> var three = "hello".substring(0, 2);
Oops, try again.
> var three = "Manabu".substring(0, 3);
> "coding rules".replace("coding", "programming")
==> programming rules
> "hello world".toUpperCase();
==> HELLO WORLD
You've completed this lesson! Start the next one.
>
view raw 4.txt hosted with ❤ by GitHub
// 1
// declare and set the variable named "today"
// to today's weekday name
var today = "Tuesday";
// 2
var array = [1, 4, 6];
var number = array[2];
// 3
var numbers = [1, 2, 3];
// 4
var days = [];
days[0] = "Monday";
days[1] = "Tuesday";
days[2] = "Wednesday";
view raw 5.js hosted with ❤ by GitHub
// 1
var number = 7;
if( number === 7 ) {
console.log( "you typed a 7!" );
}
// 2
var name = "Annie";
if (name === "Sam") {
console.log("Your name is Sam!"); }
else {
console.log();
};
// 3
var name;
// Prompt for the user's name here.
name = prompt("what's your name?");
if(name === "Sam") {
console.log("Your name is Sam!");
} else{
console.log("Your name isn't Sam!");
};
// 4
var number = prompt("Guess what number I'm thinking of between 1 and 10!");
if(number === "7") {
console.log("You got it!");
// Change the following line.
} else if (number === "6") {
console.log("Close! Try guessing a little higher.");
} else {
console.log("You were way off! Sorry...");
}
// 5
var word = "that";
if( word === "that" ) {
console.log( "word is equal to 'that'" );
}
// 6
var number = prompt( "pick a number between 1 and 10!" );
if( number === 5 ) {
console.log( "your number is 5" );
}
else if ( number > 5 ) {
console.log( "your number is greater than 5" );
}
else if ( number < 5 ) {
console.log( "your number is less than 5" );
}
// 7
var response = prompt("Do you like me?");
if (response = "yes") {
console.log("I like you too!");
}
view raw 6.js hosted with ❤ by GitHub
// 1. +1
var i = 0;
i++;
i++;
console.log( "i is equal to " + i );
// 2. -1
var i = 2;
i--;
i--;
console.log( "i is equal to " + i );
// 3. A Little Bit Faster Now
var i;
for (i = 0; i < 2; i++) {
console.log("i is now equal to " + i);
}
console.log("i is now equal to " + i);
// 4. Down Down Down
var i;
for ( i = 2; i > 0; i-- ) {
console.log( "i is now equal to " + i );
};
console.log( "i is now equal to " + i );
view raw 7.js hosted with ❤ by GitHub
// 1. Keep It Going
var i = 0;
while ( i < 2 ) {
console.log( "i is now " + i );
i++;
}
// 2. Take a While
var i = 0;
while ( i < 2 ) {
console.log( "hello" );
i++;
};
// 3. Not So Fast
var times = 0;
while (times > 0 && times < 3) {
console.log("the loop ran");
times++;
}
// 4. Do It
var i = 0;
do {
console.log("This is iteration " + (i + 1) + ".");
i++;
}
while( i < 4 );
view raw 8.js hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿