Skip to content

Commit 81d6633

Browse files
committed
Adding more
1 parent 8b13076 commit 81d6633

19 files changed

+274
-0
lines changed

array-methods.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ x.sort(function(a, b){return b - a}); // numeric descending sort
2626
highest = x[0]; // first item in sorted array is the lowest (or highest) value
2727
x.sort(function(a, b){return 0.5 - Math.random()}); // random order sort
2828

29+
concat, copyWithin, every, fill, filter, find, findIndex, forEach, indexOf, isArray, join, lastIndexOf, map, pop, push, reduce, reduceRight, reverse, shift, slice, some, sort, splice, toString, unshift, valueOf
30+
2931
/* More Array Methods */
3032

3133
// Accessing Elements I

break-continue.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
for (var i = 0; i < 10; i++) {
2+
if (i == 5) {
3+
break; // stops and exits the cycle
4+
}
5+
document.write(i + ", "); // last output number is 4
6+
}
7+
for (var i = 0; i < 10; i++) {
8+
if (i == 5) {
9+
continue; // skips the rest of the cycle
10+
}
11+
document.write(i + ", "); // skips 5
12+
}

comments.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* Multi line
2+
comment */
3+
// One line

do-while-loop.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var i = 1; // initialize
2+
do {
3+
// enters cycle at least once
4+
i *= 2; // increment to avoid infinite loop
5+
document.write(i + ", "); // output
6+
} while (i < 100); // repeats cycle if statement is true at the end

error-try-catch-throw.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
try {
2+
// block of code to try
3+
undefinedFunction();
4+
} catch (err) {
5+
// block to handle errors
6+
console.log(err.message);
7+
}
8+
9+
// Throw error
10+
11+
throw "My error message"; // throw a text
12+
13+
// Input validation
14+
15+
var x = document.getElementById("mynum").value; // get input value
16+
try {
17+
if (x == "") throw "empty"; // error cases
18+
if (isNaN(x)) throw "not a number";
19+
x = Number(x);
20+
if (x > 10) throw "too high";
21+
} catch (err) {
22+
// if there's an error
23+
document.write("Input is " + err); // output error
24+
console.error(err); // write the error in console
25+
} finally {
26+
document.write("</br />Done"); // executed regardless of the try / catch result
27+
}

error-types.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Error name values
2+
RangeError A number is "out of range"
3+
ReferenceError An illegal reference has occurred
4+
SyntaxError A syntax error has occurred
5+
TypeError A type error has occurred
6+
URIError An encodeURI() error has occurred

for-loops.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
for (var i = 0; i < 10; i++) {
2+
document.write(i + ": " + i*3 + "<br />");
3+
}
4+
var sum = 0;
5+
for (var i = 0; i < a.length; i++) {
6+
sum + = a[i];
7+
} // parsing an array
8+
html = "";
9+
for (var i of custOrder) {
10+
html += "<li>" + i + "</li>";
11+
}

functions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function addNumbers(a, b) {
2+
return a + b;
3+
}
4+
x = addNumbers(1, 2);

js-date-object.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Sat Feb 22 2020 19:31:24 GMT+0530 (India Standard Time)
2+
var d = new Date();
3+
4+
// 1582380084791 miliseconds passed since 1970
5+
6+
Number(d);
7+
Date("2017-06-23"); // date declaration
8+
Date("2017"); // is set to Jan 01
9+
Date("2017-06-23T12:00:00-09:45"); // date - time YYYY-MM-DDTHH:MM:SSZ
10+
Date("June 23 2017"); // long date format
11+
Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); // time zone
12+
13+
// Get Times
14+
15+
var d = new Date();
16+
a = d.getDay(); // getting the weekday
17+
18+
getDate(); // day as a number (1-31)
19+
getDay(); // weekday as a number (0-6)
20+
getFullYear(); // four digit year (yyyy)
21+
getHours(); // hour (0-23)
22+
getMilliseconds(); // milliseconds (0-999)
23+
getMinutes(); // minutes (0-59)
24+
getMonth(); // month (0-11)
25+
getSeconds(); // seconds (0-59)
26+
getTime(); // milliseconds since 1970
27+
28+
// Setting part of a date
29+
30+
var d = new Date();
31+
d.setDate(d.getDate() + 7); // adds a week to a date
32+
33+
setDate(); // day as a number (1-31)
34+
setFullYear(); // year (optionally month and day)
35+
setHours(); // hour (0-23)
36+
setMilliseconds(); // milliseconds (0-999)
37+
setMinutes(); // minutes (0-59)
38+
setMonth(); // month (0-11)
39+
setSeconds(); // seconds (0-59)
40+
setTime(); // milliseconds since 1970)

js-global-functions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eval(); // executes a string as if it was script code
2+
String(23); // return string from number
3+
(23).toString(); // return string from number
4+
Number("23"); // return number from string
5+
decodeURI(enc); // decode URI. Result: "my page.asp"
6+
encodeURI(uri); // encode URI. Result: "my%page.asp"
7+
decodeURIComponent(enc); // decode a URI component
8+
encodeURIComponent(uri); // encode a URI component
9+
isFinite(); // is variable a finite, legal number
10+
isNaN(); // is variable an illegal number
11+
parseFloat(); // returns floating point number of string
12+
parseInt(); // parses a string and returns an integer

0 commit comments

Comments
 (0)