Category Archives: Javascript

Dragon Slayer!

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

while (slaying) {
if (youHit) {
console.log(“You hit the dragon”);
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log(“You win!”);
slaying = false;
} else {
youHit = Math.floor(Math.random() *2 );
}

} else {
console.log(“You lose!”);
slaying = false;
}

}

@home 145, NYC

Build “Rock, Paper, Scissors

var userChoice = prompt(“Do you choose rock, paper or scissors?”);
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = “rock”;
} else if(computerChoice <= 0.67) {
computerChoice = “paper”;
} else {
computerChoice = “scissors”;
} console.log(“Computer: ” + computerChoice);

var compare = function (choice1, choice2) {

if (choice1 === choice2) {
return “The result is a tie!”;

} else if (choice1 === “rock”) {

if (choice2 === “scissors”) {
return “rock wins”;
} else {
return “paper wins”;
}

} else if (choice1 === “paper”) {
if (choice2 === “rock”) {
return “paper wins”;
} else {
return “scissors wins”;
}
} else {
if (choice2 === “rock”) {
return “rock wins”;
} else {
return “scissors wins”;
}
}

};
compare(userChoice,computerChoice);

@home 145, NYC

My first JavaScript game!

// Check if the user is ready to play!
confirm(“I am ready to play!”);

var age = prompt(“What’s your age”);
if (age < 13) {
console.log(“You are not allowed to play but you take no responsibility.”);
} else {
console.log(“Play on!”);
}

console.log(“You are at a Justin Bieber concert, and you hear this lyric ‘Lace my shoes off, start racing.'”);
console.log(“Suddenly, Bieber stops and says, ‘Who wants to race me?'”);

var userAnswer = prompt(“Do you want to race Bieber on stage?”);

if (userAnswer === “yes”) {
console.log(“You and Bieber start racing. It’s neck and neck! You win by a shoelace!”);
} else {
console.log(“Oh no! Bieber shakes his head and sings ‘I set a pace, so I can race without pacing.'”);
}

var feedback = prompt(“Please rate my game out of 10.”);
if (feedback > 8) {
console.log(“Thank you! We should race at the next concert!”);
} else {
console.log(“I’ll keep practicing coding and racing.”);
}

 

@ home 145, NYC

onChange

Question:
下拉文本框表头中 onChange=
“javascript:window.open(this.options[this.selectedIndex].value”)
其作用是什么?
还有 是什么意思? this.options[this.selectedIndex].value

<form name=”myfrom”>
<select name=”select1″ size=”1″ onChange=”javascript:window.open(this.options[this.selectedIndex].value)” >
<option selected>–搜索引擎–</option>
<option value=”http://www.hongen.com”>洪恩在线</option&gt;
<option value=”http://www.sina.com.cn”>新浪网</option&gt;
<option value=”zuoye9-1.html”>中国园丁网</option>
</select>
</form>

Answer:
window.open()打开一个新窗口
this.引用当前对像,这里指向name=”select1″的select对像
this.options引用当前的select的option集合
this.selectedIndex引用当前select的option集中被鼠标选中的项(既为反色的项)
this.options[this.selectedIndex].value 指向当前select中被选中项的值
onChange 行为,当内容发生变化时触发javascript事件

NYC @333 office

SCRIPT5007: The value of the property ‘$’ is null or undefined, not a Function object

It’s not about JQuery problem. The original <script type…> code was copied from Flowplayer.org. Their sample is error and cannot work in IE8 and IE 7 probably. The overlay pop-up window won’t work. After struggling for several hours, I finally found the solution to change the type from “application/javascript” to “text/javascript”. Now the problem is solved!

Wrong code:
<script type=”application/javascript” src=”jquery.tools.min.js”></script>

Correct code:
<script type=”text/javascript” src=”jquery.tools.min.js”></script>

@333 office