札束風呂を目指して初めてのJavaScriptを動画で学んでいく その2
今回も演習で作成したプログラムを載せていきます。
第12回前半
pushやpopなどを使って配列の中身を入れ替えるプログラム
a = ["Revin", "最", "低"];
a.pop();
a.push("高");
a.shift();
a.unshift("やられ");
if (a.join("") == "やられ最高") {
console.log("★★★正解★★★");
}
else{
console.log("×××間違い×××");
}
第12回後半
[diceCount]d[diceMax]のサイコロを表示する関数
dice=function(diceMaxa){
return Math.floor(Math.random()*diceMaxa+1);
};
diceEx=function(diceCount,diceMax){
sum=0;
diceC=1;
array=[];
while(diceC<=diceCount){
array.push(dice(diceMax));
diceC++;
}
count=0;
while(count<array.length){
sum=sum+array[count];
count++;
}
console.log(array);
console.log(diceCount+"d"+diceMax+"の結果は"+sum+"です");
};
diceEx(2,6);
第13回前半
カウントダウンプログラム
for(count=10;count>=0;count--){
console.log(count);
}
console.log("新年おめでとう!");
第13回後半
3の倍数と3が含まれる数字のときにホアーッと言うプログラム
for(count=1;count<=100;count++){
if(count%3==0){
console.log("ホアーッ!"+count);
}
else if(count%10==3){
console.log("ホアーッ!"+count);
}
else if(Math.floor(count/10)==3){
console.log("ホアーッ!"+count);
}
else{
console.log(count);
}
}
英数字を用いて8桁のパスワードを作成する関数
dice=function(diceMaxa){
return Math.floor(Math.random()*diceMaxa+1);
};
generatePassword=function(){
var a="";
var b="0123456789abcdefghijklmnopqrstuvwxyz";
for(count=0;count<8;count++){
a=a+b.substr(dice(36)-1,1);
}
return a;
};
generatePassword();
今回はここまでです。段々演習が難しくなってきましたがなんとかついていきたいと思います。