Sie sind auf Seite 1von 5

Learning Javascript

1.ConfirmandpromptWecanmakepopupboxesappear!
confirm("I am ok");prompt("Are you ok?");

2.Datatypesa.numbers(e.g.4.3,134)b.strings(e.g."dogs go
woof!","JavaScript expert")c.booleans(e.g.false,5 > 4)
3.ConditionalsIfthefirstconditionismet,executethefirstcode
block.Ifitisnotmet,executethecodeintheelseblock.Seethecode
ontherightforanotherexample.

Conclusion:Part1
Let'sdoaquickreview!
Datatypesa.numbersjustusethemlikeregularnumbersb.strings
anythingbetweena""isastring.Wordshavetobestrings.c.booleans
canonlybetrueorfalse.
VariablesWestoredatavaluesinvariables.Wecanbringbackthe
valuesofthesevariablesbytypingthevariablename.
Manipulatingnumbers&stringsa.numberscomparisonoperators,
modulob.stringslength,substring
console.log()Printsintotheconsolewhateverweputinthe
parentheses.

Some Javascript:
// Check if the user is ready to play!
confirm("Ready to Play? Bitch?");
var age = prompt("Your age, Sir or Madam?");
if(age<18)
{
console.log("You're allowed to play, but you take no responsibility");
}
else{
console.log("LET'S PLAY now :D");
}
console.log("Snow White and Batman were hanging out at the bus stop,
waiting to go to the shops. There was a sale on and both needed some
new threads. You've never really liked Batman. You walk up to him.");
console.log("Batman glares at you.");
var userAnswer = prompt("Are you feeling lucky, punk?");
if(userAnswer == "yes"){
console.log("Batman hits you very hard. It's Batman and you're you! Of
course Batman wins!");
}
else{
console.log("You did not say yes to feeling lucky. Good choice! You are a
winner in the game of not getting beaten up by Batman.");
}
var feedback = prompt("Rate the game out of 10");
if(feedback > 8){
console.log("This is just the beginning of my game empire. Stay tuned
for more!");
}

else console.log("I slaved away at this game and you gave me that score?!
The nerve! Just you wait!");

Functions:
// This is what a function looks like:
var divideByThree = function (number) {
var val = number / 3;
console.log(val);
};
var greeting = function (name) {
console.log("Great to see you," + " " + name);
};
/*jshint multistr:true */
var text = "So this other day Faris ate a burger";
var myName = "Faris";
var hits = [];
for (var i = 0;i<text.length;i++){
if(text[i]==myName[0])
{
for(var j = i;j<i+myName.length;j++)
{
hits.push(text[j]);
}
}
}
if(hits.length==myName.length)
console.log(hits);
else console.log("Your name wasn't found!");

var understand = true;


while(understand === true){
console.log("I'm learning while loops!");
understand = false;
}
console.log("I just finished from the while loop!");
//when comparing with true, we must use triple =

Sometimesyouwanttomakesureyourlooprunsatleastonetimeno
matterwhat.Whenthisisthecase,youwantamodifiedwhileloop
calledado/whileloop.
var randomNumber = Math.random();
Math.floor(Math.random() * 5 + 1);

willgeneratearandomnumberbetween1and5,inclusive.

Das könnte Ihnen auch gefallen