-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode2_java.html
154 lines (140 loc) · 4.33 KB
/
code2_java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<html>
<head>
<title>My second html perros!</title>
</head>
<body>
<h1>JavaScript examples</h1>
<div>
<p>El texto, the text, die farrat ist rot</p>
<p>console.log() to print in consola Java</p>
</div>
</body>
<script>
console.log("Showing how to display math operations in console");
console.log(2+7);
console.log(Math.PI);
console.log(Math.E);
//Global variables
a=1; //Number
b="1"; //String
c=[0, 1, "4", "lala", [5,5]]; //Array of two numbers, a string and an array
f=false; //Boolean
f=3.2; //Redefine a variable
console.log("c[0]", c[0]);
console.log("c[1]", c[1]);
console.log("c[2]", c[2]);
console.log("c[3]", c[3]);
console.log("c[4]", c[4]);
console.log("Array length", c.length);
//Local variables
var name="Pepe";
var age=20;
var lasuma=1+6;
console.log("original", lasuma)
lasuma+=1;
console.log("+=", lasuma)
lasuma ++;
console.log("++", lasuma)
lasuma--;
console.log("--", lasuma)
var ladivi=lasuma/2
console.log("lasuma/2", ladivi)
var mod=ladivi%2;
typeof(mod); //Returns type of data
var type=typeof(mod);
console.log(type);
var concat=name+" pepino";
console.log(concat);
var nested=[[1,2], [3,4], [10,11]];
console.log("second element of nested:", nested[1]);
c.push(3); //Add the element 3 as number to the array
console.log("Array length new", c.length);
console.log(c);
c.pop();//Remove last element of the array
console.log(c);
var p=c.indexOf("4");
console.log(p);
//console.log(c.indexOf(2));
</script>
<!--Script #2 about objects and another mkdas-->
<script>
//Create objects
obj={key1:3, key2:4}; //key1 and 2 are object's properties
console.log("Value key1", obj.key1);
console.log("Value key2", obj["key2"]);
obj2={stringk1:"3", stringk2:"40"};
console.log("Key1 as string obj 2", obj2.stringk1)
obj2.key3="LA key" //Adding characteristics to object
console.log("Value new key", obj2.key3)
</script>
<!--Control sequences-->
<script>
//IF
n="2"
if(parseFloat(n)==1){
console.log("if");
} else if (parseFloat(n)==2){
console.log("else if");
} else{
console.log("else");
}
//TERNARY - condition ?, what happens if true, :, what happens if false
parseFloat(n)/2==1 ? console.log("condition is true"): console.log("condition is false");
//SWITCH
var i="something";
switch(i){
case "anything":
console.log("No the same");
break;
case "something":
console.log("Same");
break;
default:
console.log("Default")
}
</script>
<!--Loops-->
<script>
//FOR
var out="";
for(i=0; i<10; ++i){
out +=i +", "; //Concatenates numbers in a string
}
console.log("For loop", out);
//WHILE
var j=0.5;
while(j<10){
out +=j + ", ";
j=j*2
}
console.log("While loop", out);
</script>
<!--Functions-->
<script>
//Writing a function
function aFunction(v){
if(v<10){
return v;
} else{
return v*v;
}
}
//Using the function
console.log("Using for v=5", aFunction(5));
console.log("Using for v=30", aFunction(30));
//Javascript let's do stupid things(?)
console.log("with a string", aFunction("Pepa"));
console.log("With more parameters", aFunction(2,6));
console.log("Moreee stuff", aFunction(2, "ja"));
console.log("W/o a thing", aFunction());
//Another way of using functions
var variableFunction=function(v){
if (v>10){
return "big";
} else{
return "small";
}
}
console.log("The number is", variableFunction(233));
</script>
</html>