Skip to content

Commit

Permalink
Javascript Fundamentals
Browse files Browse the repository at this point in the history
  • Loading branch information
CycloSiphon committed Feb 4, 2023
1 parent 24e66d7 commit 0498e62
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 0 deletions.
16 changes: 16 additions & 0 deletions JSExercises/new1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<script>
// Add 2 decimal numbers
var a = 2.3;
var b = 3.3;
console.log(a+b);

// Or

var c = 2.0;
var d = 4.0;
console.log((c+d).toFixed(1))
</script>
</head>
</html>
41 changes: 41 additions & 0 deletions JSExercises/new10.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<html>

<head>
<script>
// Sorting

// let arrN = [2, 9, 3, 7, 20, 5];
// let arrS = ['Jan', 'Feb', 'December', 'July', 'April'];

// arrN.sort((a,b)=> a-9);
// arrN.sort(function cmp(a,b){

// });
// console.log(arrN);

// arrS.sort();
// arrS.reverse();
// console.log(arrS);

//Object Creation using Object Constructor

function Objt(a, b, c, d) {
this['Objt1'] = a;
this.Objt2 = b;
this.Objt3 = c;
this.Objt4 = d;

}
Objt.ky = 5;

let vl = new Objt(1, 4, 6, 2);
console.log(vl);

console.log(this);

console.log(vl.ky);

</script>
</head>

</html>
41 changes: 41 additions & 0 deletions JSExercises/new11.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<html>

<head>
<script>
// Sorting Method 1

let arrN = [2, 9, 3, 7, 20, 5];

function cmp(a, b) {
return a < b ? -1 : a > 2 && a < 6 ? -1 : 0;
}

console.log(arrN.sort(cmp));


// Sorting Method 2

let arrN2 = [2, 9, 3, 7, 20, 5];

arrN2.sort((a,b)=>a-b);

let n = Math.ceil(arrN2.length/2);

let arrSplcd = arrN2.splice(0,n);

let arrSplcd2 = arrN2.splice(n,arrN2.length);

arrSplcd.reverse();

let mrgdArr = [...arrSplcd,...arrN2];

console.log(mrgdArr);





</script>
</head>

</html>
38 changes: 38 additions & 0 deletions JSExercises/new12.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>

<head>
<script>
// Bind, Call, Apply

let Objt = {
'key1':'abc',
'key2': 2,
'key3':function(a,b){
// return this.key1 +' '+ this.key2 + ' ' + a + ' ' + b;
return `${this.key1} ${this.key2} ${a} ${b} word`;
}
}

let Objt2 = {
'key1': 'dce',
'key2': 5
}


let Objtv = Objt.key3.apply(Objt2,[6,8]);

// let Objt2int = new Objt2();

// let Objtint = new Objt(Objt2int);



console.log(Objtv);




</script>
</head>

</html>
29 changes: 29 additions & 0 deletions JSExercises/new13.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>

<head>
<script>
// Class


class fnc{
constructor(name,age){
this.name = name;
this.age = age;
}

fn(){
let r = 30/2;
console.log(r);
}
}

let oFnc = new fnc('sa',100);

oFnc.fn();



</script>
</head>

</html>
18 changes: 18 additions & 0 deletions JSExercises/new14.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<html>

<head>

<script type="module">
//Modules

let v1 = 5;
let v2 = 10;

export { v1, v2 }


</script>
</head>

</html>
4 changes: 4 additions & 0 deletions JSExercises/new15.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { v1, v2 } from './new14.js'

console.log(v1);
console.log(v2);
18 changes: 18 additions & 0 deletions JSExercises/new16.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>

<head>

<script type="module">

// Import Export

import { v1, v2 } from './new14.html'

console.log(v1);
console.log(v2);


</script>
</head>

</html>
39 changes: 39 additions & 0 deletions JSExercises/new17.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<html>

<head>

<script>
//Error Handling

let v1 = 5;
let v2 = 6;

let sum = () => v1 + v2;

try {
if (sum() === 11) {
throw {
'name': 'abc',
'message': '11 is an error'
};

}
// sum();

}

catch (err) {
console.log(err);
console.log(err.name);
console.log(err.message);
}

finally {
console.log('Code Executed');
}

</script>

</head>

</html>
10 changes: 10 additions & 0 deletions JSExercises/new2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<script>
// Multiply string by a number
var a = '34';
var b = 40;
console.log(a*b);
</script>
</head>
</html>
24 changes: 24 additions & 0 deletions JSExercises/new3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>

<head>
<script>
// Compare number and print if it is a decimal
var n = 13.2;

if ((n - Math.floor(n)) !== 0)
console.log(n + ' has a decimal place.');
else
console.log(n + ' is a whole number.');

//OR
var m = 17.8;

if (Math.floor(m) !== m)
console.log(m + ' has a decimal place.');
else
console.log(m + ' is a whole number.');

</script>
</head>

</html>
28 changes: 28 additions & 0 deletions JSExercises/new4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html>

<head>
<script>
// Array to add & remove elements

const items = ['table','chair','desk'];
console.log(items);

// items.pop();
// console.log(items);

// items.push('bottle');
// console.log(items);

items.unshift('umbrella');
console.log(items);

items.shift();
console.log(items);

// items.splice(1,1,'basket');
// console.log(items);

</script>
</head>

</html>
19 changes: 19 additions & 0 deletions JSExercises/new5.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>

<head>
<script>
// Randomly add elements to an array

const Ritems = [];
const items = ['table', 'chair', 'desk', 'bottle', 'pen', 'pencil'];

while (items.length) {
Ritems.splice(Math.floor(Math.random() * (Ritems.length + 1)), 0, items.pop());
}

console.log(Ritems);

</script>
</head>

</html>
44 changes: 44 additions & 0 deletions JSExercises/new6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<html>

<head>
<script>

let obj = {
"key1": 123,
"key2": true,
"key3": `abc`
}


let arrObj = Object.entries(obj);

// arrObj[1].map((curVal)=> console.log(curVal));

console.log(arrObj);

// arrObj.map(function (curVal, index, arr){
// for (i=0; i<2; i++){
// console.log(arrObj[index][i]);
// }
// })


arrObj.map(function(curVal,index,arr){
curVal.map(function(cuVal){
console.log(cuVal);
}
)
})

// for (i = 0; i < 2; i++) {
// for (j = 0; j < 2; j++) {
// console.log(arrObj[i][j]);
// }
// }



</script>
</head>

</html>
Loading

0 comments on commit 0498e62

Please sign in to comment.