-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemojs.js
107 lines (93 loc) · 2.93 KB
/
demojs.js
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
function paragraph() {
document.getElementById("paragraph").innerHTML = "Paragraph changed.";
}
function myfunction() {
var price1 = 5;
var price2 = 6;
var total = price1 + price2;
console.log(total);
// You can create a const object:
const car = { type: "Fiat", model: "500", color: "white" };
// You can change a property:
car.color = "red";
// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
var x = 5;
var z = Math.pow(x, 2); // result is 25
console.log(z);
var x = 16 + 4 + "Volvo";
console.log("16 + 4 + Volvo = "+x);
var x = "Volvo" + 16 + 4;
console.log("Volvo + 16 + 4 = " + x);
typeof "John" // Returns "string"
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof false // Returns "boolean"
typeof x // Returns "undefined" (if x has no value)
typeof { name: 'John', age: 34 } // Returns "object"
typeof [1, 2, 3, 4] // Returns "object" (not "array", see note below)
typeof null // Returns "object"
typeof function myFunc() { } // Returns "function"
}
function mult(p1, p2) {
return p1 * p2;
}
function myfunction2() {
var person = {
firstName: "John",
lastName: "Doe",
id: 5566,
fullName: function () {
return this.firstName + " " + this.lastName;
}
};
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude);
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
console.log(checkCookie);
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}