Google

TECHHOT

色々書いてます。プログラミングのことも使えるかなと思ったら書きます。個人でブログとは関係なく運営しているワークショップのことについてもいずれはまとめて書いていきます。

スポンサーリンク

codecademyで学習#第1話

どうもこんにちは。
今週もプログラミングを終え一息ついてるあんみつです。

最近暑くなってきて私の私服もハワイアンシャツ一択。


毎週プログラミングをやると決めている私が使っているサイトはcodecademyです。

www.codecademy.com

このサイトはJavascript,pyhon,phpなどをブラウザ上で学習できるサービスです。


"what is your name?".length


このようにホント初歩的な学習から始まります。また、最初から全員ができる人ではないので、初歩的なところでもつまずく場合は質問ができるようにQ&Aフォーラムもあるのでわからないところはわからないままにせず学習できます。

console.log(2*5);
console.log("Hello");


console.logは出力を表しているのですがそれをわかりやすく説明するために


Using console.log


You may have noticed that the interpreter doesn't print out every single thing it does. So if we want to know what it's thinking, we sometimes have to ask it to speak to us.

console.log() will take whatever is inside the parentheses and log it to the console below your code—that's why it's called console.log()!

This is commonly called printing out.

ざっくり翻訳すると...


あなたは翻訳がすべてのものを出力しないと気付いたかもしれません。
それが何を考えているのか知りたいなら、私たちはそれに話しかけるように時々頼まなければなりません。

console.log()かっこ内部にあるものは何でも出力します。そしてそれがconsole.logと呼ばれている理由はあなたのコードの下のconsoleへ括弧内部を記録するからです。

これはプリントアウトとも呼ばれます。

こんな細かく出力だけで説明してくれるプログラミング学習サービスが日本のウェブサービスにあるでしょうか?

日本の場合はconsole.logは出力に使うんだよ。で終わりですがやはりプログラミングの最先端を行っている海外のウェブは違いますね。

深く教えてくれるので確実に身になっているのがわかります。

「Choose Your Own Adventure 2!」(1/6)

さて私が今日学んだパートは「Choose Your Own Adventure 2!」です。この分野は6個学習要素がありました。

まずは1/6

What you'll be building


We told you this was just the beginning of our game empire! Now that you know more about JavaScript, you'll be able to create a much richer "choose your own adventure" game.

まあ、ゲームを作れるよっていってます。
コードはざっくりこんな感じ。

var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();

switch(troll) {
  case 'FIGHT':
    var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
    var smart = prompt("Are you smart?").toUpperCase();
    if(strong === 'YES' || smart === 'YES') {
      console.log("You only need one of the two! You beat the troll--nice work!");
    } else {
      console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
    }
    break;
  case 'PAY':
    var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
    var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
    if(money === 'YES' && dollars === 'YES') {
      console.log("Great! You pay the troll and continue on your merry way.");
    } else {
      console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
    }
    break;
  case 'RUN':
    var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
    var headStart = prompt("Did you get a head start?").toUpperCase();
    if(fast === 'YES' || headStart === 'YES') {
      console.log("You got away--barely! You live to stroll through the forest another day.");
    } else {
      console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you.");
    }
    break;
  default:
    console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
}


ストーリは主人公は森を歩いていてそこでトロールにであって戦うか逃げるか賭けるかまずは選択してね♡っていう簡単な条件分岐ゲームです。

このプログラムを見て理解できた人はすごいですがjavascriptを見たことがない人がこれを見ると何やってんだこれ?(笑)ってなりますよね。

「Choose Your Own Adventure 2!」(2/6)

ここでは、条件分岐ゲームに入る前に使う機能のpromptの復習をするところですね。


Prompt


First, we'll need to use a prompt statement to ask our user what he or she wants to do. Recall that we use prompt like this:
We store the result of using prompt in a variable so we can use the user's response to influence what the program does.

ここでざっくり、promptは変数にユーザーが入力した文字や数値を格納するよって言ってます。

var user=prompt("did you like this?");

まあこんな感じで宣言しました。

「Choose Your Own Adventure 2!」(3/6)

ここでやっと大事な学習ポイントが出てきます。

You may have noticed us use the .toUpperCase() function in the first exercise. We used it like this:

var answer = prompt("Question to the user");

This converted the user's answer to ALL CAPS before saving it in the answer variable. This helps eliminate problems that might crop up if your program tests for 'YES' but your user typed in 'yes' or 'Yes'. The input becomes all caps before we test, so we only have to test for all caps!

You can also use .toLowerCase(), which converts a string to all lower-case letters.

またもここでざっくり、すべての文字をALL CAPS(すべて大文字)に変えるよ!例えばyes→YESとかね(ゝc_,・。)

って感じで

var user=prompt("did you like this?").toUpperCase();

「Choose Your Own Adventure 2!」(4/6)

swithの復習ですねわかります。

ここではswithの学習をしました。

var user=prompt("did you like this?");
switch(user){
    case 'YES':
        console.log("hehehehheh");
        break;
    case 'NO':
        console.log("fuck");
        break;
    case 'nnnn':
        console.log("kooko");
        break;
    default:
        console.log("nnnn");
    }

ってなかんじで宣言して終わり。
(~上のコードの説明~
 ケース1:さとし君.あなたはこれ好きですか?
     タケル君.イェスウ!!
     さとし君.へへっへへへへ!
 
 ケース2:さとし君.あなたはこれ好きですか?
     タケル君.ノオウ!
     さとし君.ファ〇ク
 
 ケース3:さとし君.あなたはこれ好きですか?
     タケル君.んんんんビクビク
     さとし君.こおおこ
 
 ケース4:さとし君.あなたはこれ好きですか?
     タケル君.(あなたの答え)
     さとし君.んんんん
ちょっと頭おかしいんですかね?このコード書いた人。
(※本人です)

「Choose Your Own Adventure 2!」(5/6)

ここではこの分野での総まとめです。

if/elseとswith/caseと演算子(||または&&かつ)を使って簡単なゲームを作ろうって問題です。

ar user=prompt("did you like this? and your old is 20").toUpperCase();
switch(user){
    case 'YES':
        var value=prompt("500yen or 600yen that you want , 500 or 600");
        var number=prompt("1 or 2 that you want,1 or 2");
        if(value===500&&number===1){
            console.log("you need 500 yen");
        }
        else{
            console.log("you need whitch 600yen,1000yen,1200yen is")
            }
        break;
    case 'NO':
       var other=prompt("there are strawberry,kiwi fruit,cherry here,all thing is 100yen");
       var number=prompt("what is you want other number?,1~3");
       if(other===cherry){
           console.log("are you cherry? awesome!!");
           if(number1===1){
           console.log("ok your old is one");
            }
           else if(number===2){
           console.log("ok your old is two");
            }
           else{
           console.log("don't worry");
               }
           }
        else{
            console.log("fuck off");
            }
        break;
    case 'NNNN':
        var language=prompt("I didnt catch what you say that is Japanese or English?").toUpperCase();
        if(language===JAPANESE||language===ENGLISH){
        console.log("ok,kooko");
        }
        else{
            console.log("what?!");
            }
        break;
    default:
        console.log("nnnn");
    }

このコード見てまだまだ甘いなと思う人もいるし、なんだこいつき〇がいかよって感じる人もいるでしょう。自分でも全然足りていないところも多いのでこれから徐々に焦らず勉強していきますわい。
※6/6は問題ではないので省略

今週の学習ポイント

今週の学習ポイントは.toUpperCase()ですね。これはすべての文字を大文字に変えてくれる素敵な機能です。

promptで入力しその入力された文章を全部大文字にするのでまさに手間いらず。

.toLowerCaseもあるようですね。これは、前者の逆の意味でしょう。


てな感じで今週も終わりました。お腹一杯です。炒飯食べよ。