JavaScript for beginners | LBOET | HOXFRAMEWORK
Hello and welcome to another tutorial, in this one we will
be learning a little bit about JavaScript!
To write javascript you need an html file where it can be
executed.
<!--
HTML CODE:
<script src="1.js"></script>
-->
And then there is the JS code: (ignore the ! and <> stuff)
<!--
alert("Welcome user");
document.write("<h2>Hey friend</h2>");
var name = window.prompt("Enter your name: ");
alert("Your name is " + name);
//datatypes
var nickname = "Tord";
var position = "boss";
var age = 22;
var metersTall = 1.8;
document.write(position + " " + nickname + " is " + age + " years old and " + metersTall + " tall.");
document.write("<hr><br />");
document.write( 10 + Number("2") + "<hr>");
document.write( 10 + parseInt("5") + "<hr>");
document.write( 10 + parseFloat("3.14") + "<hr>")
//However there is a difference between a Number and an Integer
//when you parseInt("tom99") it will return 99
//but when you do Number("tom99") it returns a NaN
var isNull;
isNull = false;
isNull = true;
//Arrays
var someArray = [3,4,5,"twenty",false];
someArray[0] = 90;
someArray.push("33"); //how to add/append to the array
//multidimensional array
var someMultiDimArray = [[1,2],[3,4]];
someMultiDimArray[0][1] = 99;
document.write("<font color='red'>" + someMultiDimArray[0][1] + "</font> <br />");
//OBJECTS (in python this would be dictionaries)
var site = {
host: "Google",
ip: "127.0.0.1",
};
site.host = "Home"
document.write("<br> A warm place called " + site.host + "<br>");
document.write("has the ip :" + site.ip + "<br>");
//FUNCTIONS
var sum = plus(5,7);
document.write(sum + "<br>");
function plus(one,two){
return one + two
};
var saying = lyric("There once was a hero named Ragnar the red");
function lyric(songlyric){
document.write("The song goes :" + songlyric);
};
//notice how we added the variable to the function
//BEFORE we defined the function
//IF STATEMENTS
var pet = "dog";
if (pet == "dog"){
document.write("<ul> its true you have a <li> dog </li> </ul>");
}
else if(pet && !secondpet){
document.write("You dont have a second pet, your first pet isnt a doggo but you have a first pet");
}
else {
document.write("Either you dont have a first pet, or you have a second pet.");
};
//WHILE LOOPS
var somenumber = 1;
while(somenumber <= 3){
document.write("While loop says : " + somenumber + "<br/>");
somenumber++;
};
//FOR LOOPS
document.write("<br/>")
for(var i = 0; i < 5; i++){
document.write(i + ". Thing : No" + "<br/>");
};
//1.define a variable ( i )
// set a while condition in it, so while its under 5
// add to that variable until its your number 5
//EXCEPTIONS
try {
var x = 9;
document.write(x + 10 + "<br/>");
throw "sum ting wong?" ;
}catch(err){
document.write(err)
}finally{
//this one gets executed always.
document.write("<br />Done.")
};
// CLASSES
class Book{
constructor(title,author){
this.title = title;
this.author = author;
}
readBook(){
document.write("Reading " + this.title +" by "+ this.author)
}
}
var book1 = new Book("Harry Potter ","Jk rolin")
document.write(book1.title + "<br>");
book1.readBook();
-->
Comments