diff --git a/assignment1/Type_conversions/q1.js b/assignment1/Type_conversions/q1.js new file mode 100644 index 0000000..ae76e06 --- /dev/null +++ b/assignment1/Type_conversions/q1.js @@ -0,0 +1,15 @@ +let age=25; +if((typeof age)==number && age>0){ + if (age<=12){ + console.log("child") + } + else if(age>=13 && age<=18){ + console.log("teen") + } + else{ + console.log("adult") + } +} +else{ + console.log("invalid age") +} \ No newline at end of file diff --git a/assignment1/Type_conversions/q2.js b/assignment1/Type_conversions/q2.js new file mode 100644 index 0000000..bd6f006 --- /dev/null +++ b/assignment1/Type_conversions/q2.js @@ -0,0 +1,21 @@ +let age=25 + +switch(age){ + case (age<=12):{ + console.log("child") + break; + } + case (age>=13 && age<=18):{ + console.log("teen") + break; + } + case (age>18):{ + console.log("adult") + break; + } + default :{ + console.log("invalid age!") + break; + } + +} \ No newline at end of file diff --git a/assignment1/Type_conversions/q3.js b/assignment1/Type_conversions/q3.js new file mode 100644 index 0000000..f4d44ea --- /dev/null +++ b/assignment1/Type_conversions/q3.js @@ -0,0 +1,9 @@ +let arraySize=25; + +let arr=[] + +for(let i=0; ielement.email); +console.log(emailArr) + \ No newline at end of file diff --git a/assignment1/arrays/q3.js b/assignment1/arrays/q3.js new file mode 100644 index 0000000..2e91a29 --- /dev/null +++ b/assignment1/arrays/q3.js @@ -0,0 +1,61 @@ +// Use Array.sort to return the array sorted in descending order by date_of_birth. +let arr=[{ + "id": 1, + "first_name": "Nicki", + "email": "ncrozier0@squarespace.com", + "date_of_birth": "2009/05/09" + }, { + "id": 2, + "first_name": "Raychel", + + "email": "rmcgrady1@cpanel.net", + "date_of_birth": "1996/11/05" + }, { + "id": 3, + "first_name": "Demetris", + "email": "dkilshall2@elpais.com", + "date_of_birth": "2018/12/31" + }, { + "id": 4, + "first_name": "Amata", + "email": "abraiden3@canalblog.com", + "date_of_birth": "2012/05/23" + }, { + "id": 5, + "first_name": "Venita", + "email": "vheap4@clickbank.net", + "date_of_birth": "2020/10/04" + }, { + "id": 6, + "first_name": "Fairfax", + "email": "fcrichton5@merriam-webster.com", + "date_of_birth": "2009/12/23" + }, { + "id": 7, + "first_name": "Kathleen", + "email": "kvasyukhnov6@devhub.com", + "date_of_birth": "2010/12/20" + }, { + "id": 8, + "first_name": "Sam", + "email": "scorck7@sitemeter.com", + "date_of_birth": "2020/08/30" + }, { + "id": 9, + "first_name": "Virgilio", + "email": "vferandez8@e-recht24.de", + "date_of_birth": "2000/09/07" + }, { + "id": 10, + "first_name": "Townie", + "email": "tpetyt9@upenn.edu", + "date_of_birth": "2018/09/01" + }]; + + +console.log("before sorting in descending order on the basis of date of birth") +console.log(arr) +arr.sort((a, b) => new Date(b.date_of_birth) - new Date(a.date_of_birth)); +console.log("after sorting") +console.log(arr) + \ No newline at end of file diff --git a/assignment1/arrays/q4.js b/assignment1/arrays/q4.js new file mode 100644 index 0000000..07acc0f --- /dev/null +++ b/assignment1/arrays/q4.js @@ -0,0 +1,67 @@ +// Write a function getById that accepts a number as a parameter and returns the +// object where the id is equal to that number. + +let arr=[{ + "id": 1, + "first_name": "Nicki", + "email": "ncrozier0@squarespace.com", + "date_of_birth": "2009/05/09" + }, { + "id": 2, + "first_name": "Raychel", + + "email": "rmcgrady1@cpanel.net", + "date_of_birth": "1996/11/05" + }, { + "id": 3, + "first_name": "Demetris", + "email": "dkilshall2@elpais.com", + "date_of_birth": "2018/12/31" + }, { + "id": 4, + "first_name": "Amata", + "email": "abraiden3@canalblog.com", + "date_of_birth": "2012/05/23" + }, { + "id": 5, + "first_name": "Venita", + "email": "vheap4@clickbank.net", + "date_of_birth": "2020/10/04" + }, { + "id": 6, + "first_name": "Fairfax", + "email": "fcrichton5@merriam-webster.com", + "date_of_birth": "2009/12/23" + }, { + "id": 7, + "first_name": "Kathleen", + "email": "kvasyukhnov6@devhub.com", + "date_of_birth": "2010/12/20" + }, { + "id": 8, + "first_name": "Sam", + "email": "scorck7@sitemeter.com", + "date_of_birth": "2020/08/30" + }, { + "id": 9, + "first_name": "Virgilio", + "email": "vferandez8@e-recht24.de", + "date_of_birth": "2000/09/07" + }, { + "id": 10, + "first_name": "Townie", + "email": "tpetyt9@upenn.edu", + "date_of_birth": "2018/09/01" + }]; + +let arr5=[] +function getById(id){ + for(const element of arr){ + if(element.id==id){ + arr5.push(element) + } + } +} +console.log("elements by id") +getById(5) +console.log(arr5) \ No newline at end of file diff --git a/assignment1/arrays/questions.txt b/assignment1/arrays/questions.txt new file mode 100644 index 0000000..2cd51be --- /dev/null +++ b/assignment1/arrays/questions.txt @@ -0,0 +1,71 @@ +Assignments: +[{ +"id": 1, +"first_name": "Nicki", +"email": "ncrozier0@squarespace.com", +"date_of_birth": "2009/05/09" +}, { +"id": 2, +"first_name": "Raychel", + +"email": "rmcgrady1@cpanel.net", +"date_of_birth": "1996/11/05" +}, { +"id": 3, +"first_name": "Demetris", +"email": "dkilshall2@elpais.com", +"date_of_birth": "2018/12/31" +}, { +"id": 4, +"first_name": "Amata", +"email": "abraiden3@canalblog.com", +"date_of_birth": "2012/05/23" +}, { +"id": 5, +"first_name": "Venita", +"email": "vheap4@clickbank.net", +"date_of_birth": "2020/10/04" +}, { +"id": 6, +"first_name": "Fairfax", +"email": "fcrichton5@merriam-webster.com", +"date_of_birth": "2009/12/23" +}, { +"id": 7, +"first_name": "Kathleen", +"email": "kvasyukhnov6@devhub.com", +"date_of_birth": "2010/12/20" +}, { +"id": 8, +"first_name": "Sam", +"email": "scorck7@sitemeter.com", +"date_of_birth": "2020/08/30" +}, { +"id": 9, +"first_name": "Virgilio", +"email": "vferandez8@e-recht24.de", +"date_of_birth": "2000/09/07" +}, { +"id": 10, +"first_name": "Townie", +"email": "tpetyt9@upenn.edu", +"date_of_birth": "2018/09/01" +}] +1. Take the above array of objects. Accomplish the following tasks: +○ Write a function filterByName that accepts a string as a parameter and returns an +array with only those objects where the first_name field includes that string. + +○ Use Array.map to return an array of all the email fields. +○ Use Array.sort to return the array sorted in descending order by date_of_birth. +○ Write a function getById that accepts a number as a parameter and returns the +object where the id is equal to that number. + +2. What makes a method mutating or non mutating in Javascript? Find out whether each of +the following methods are mutating or non-mutating. How can you verify this?: +○ push +○ pop +○ filter +○ find +○ sort +○ map + diff --git a/assignment1/functions/q1.js b/assignment1/functions/q1.js new file mode 100644 index 0000000..9f8b278 --- /dev/null +++ b/assignment1/functions/q1.js @@ -0,0 +1,21 @@ +function main(){ + for(let i=0; i<5; i++){ + if(i==3){ + break; + } + } + console.log("after break statement function continues") +} + +function main1(){ + for(let i=0; i<5; i++){ + if(i==3){ + return "exited from function" + } + } + console.log("this will never printed after return statement executed.") +} + +main() +let st=main1(); +console.log(st) \ No newline at end of file diff --git a/assignment1/functions/q2.js b/assignment1/functions/q2.js new file mode 100644 index 0000000..50b39dd --- /dev/null +++ b/assignment1/functions/q2.js @@ -0,0 +1,17 @@ +function test(callback){ + callback(); +} + +function add(){ + console.log("calling the callback function") + return 5; +} + +console.log("only add") +test(add) +console.log("only add()") +test(add()) // it gives error because here add() return 5 before calling test +// and then test(5) is called but here 5 is not a function so it gives typeError. + +console.log("only ()=>add()") +test(()=>add()) diff --git a/assignment1/functions/q3.js b/assignment1/functions/q3.js new file mode 100644 index 0000000..59eb9da --- /dev/null +++ b/assignment1/functions/q3.js @@ -0,0 +1,21 @@ +// question 2 using return statement in test function before callback(); + +function test(callback){ + return callback(); +} + +function add(){ + console.log("calling the callback function") + return 5; +} + +console.log("only add") +let r1=test(add) +console.log(r1) +// console.log("only add()") +// test(add()) // it gives error because here add() return 5 before calling test +// and then test(5) is called but here 5 is not a function so it gives typeError. + +console.log("only ()=>add()") +let r2=test(()=>add()) +console.log(r2) diff --git a/assignment1/functions/questions.txt b/assignment1/functions/questions.txt new file mode 100644 index 0000000..4fae815 --- /dev/null +++ b/assignment1/functions/questions.txt @@ -0,0 +1,23 @@ +1. What happens when you add a for loop/while loop/switch case block inside a function +and use return instead of break? Do statements after the loop run? What is the return +value? Can we pass a return value from within a loop? Can you return from inside an if +block? What impact does that have? + + + + +2. Take a function that accepts a function as a parameter (also known as callback function). +function test(callback){ +callback(); +} +function callbackFunc(){ +console.log(“Calling the callback function”) +return 5; +} +Explore the following cases - what is printed in the console, and what gets returned: +○ test(callbackFunc) +○ test(callbackFunc()) +○ test(() => callbackFunc()) + +○ What happens when you return callback() from the test function? +○ What happens when you return callback from the test function? \ No newline at end of file diff --git a/assignment1/objects/q1.js b/assignment1/objects/q1.js new file mode 100644 index 0000000..881fa38 --- /dev/null +++ b/assignment1/objects/q1.js @@ -0,0 +1,22 @@ +// How will you create a new copy of the object below while updating the value of +// address.details[0] to “5“? + + +let obj = { + name: "Harry Potter", + age: 12, + address: { + details: ["4", "Privet Drive"], + area: "Little Whinging", + city: "Surrey", + state: "England" + } +} + +let obj1={...obj} + +obj1.address.details[0]=5 + +console.log(obj) +console.log("The modified code") +console.log(obj1) \ No newline at end of file diff --git a/assignment1/objects/q2.js b/assignment1/objects/q2.js new file mode 100644 index 0000000..ebf8139 --- /dev/null +++ b/assignment1/objects/q2.js @@ -0,0 +1,27 @@ +// Write a function filterObj that will filter out all the keys of a flat object that have +// objects or arrays using Object.keys and Object.entries. Example: + +let obj = { + a: "Apple", + b: ["Basketball", "Baseball"], + c: { + call: "cellphone" + + }, + d: "Dog" +} + +let result =[] +function filterObject(){ + + let obj1=Object.entries(obj) + + for(const element of obj1){ + if(typeof element[1]!="object"){ + result.push(element) + } + } +} +filterObject(); + +console.log(Object.fromEntries(result)); diff --git a/assignment1/operators/q1.js b/assignment1/operators/q1.js new file mode 100644 index 0000000..3ecab4d --- /dev/null +++ b/assignment1/operators/q1.js @@ -0,0 +1,13 @@ +// what is the difference between ++i and i++ ? + +console.log("log i using i++") +for (let i=0; i<5; i++){ + console.log(i++) +} +// i++ , it first prints the value of i and then increment it by 1; + +console.log("log i using ++i") +for (let i=0; i<5; i++){ + console.log(++i) +} +// ++i , it first increment and then print the value of i; \ No newline at end of file diff --git a/assignment1/operators/q2.js b/assignment1/operators/q2.js new file mode 100644 index 0000000..565a433 --- /dev/null +++ b/assignment1/operators/q2.js @@ -0,0 +1,11 @@ +//2. What do you think would happen if you pass an index beyond the range of the string? Or +// if you pass a negative index? Try it out. + +console.log("index beyond range of string.") +let st="ashok" +console.log(st[5]); + +let arr=[1,2,3] +console.log(arr[-1]) + +// when we try to access string index beyond range of string and array index negative then we get undefined value. diff --git a/assignment1/question1/file.JS b/assignment1/question1/file.JS new file mode 100644 index 0000000..9db221a --- /dev/null +++ b/assignment1/question1/file.JS @@ -0,0 +1 @@ +console.log("javascript file is inserted in src using Script tag in html") \ No newline at end of file diff --git a/assignment1/question1/html.txt b/assignment1/question1/html.txt new file mode 100644 index 0000000..f0860a9 --- /dev/null +++ b/assignment1/question1/html.txt @@ -0,0 +1,14 @@ +// Js is written in script tag in html file. + + + + + + Document + + + + + \ No newline at end of file diff --git a/assignment1/question1/html2.txt b/assignment1/question1/html2.txt new file mode 100644 index 0000000..ae6e3f2 --- /dev/null +++ b/assignment1/question1/html2.txt @@ -0,0 +1,13 @@ +// External Js file is inserted in HTML using script tag + + + + + + Document + + + + + \ No newline at end of file diff --git a/assignment1/question1/question.txt b/assignment1/question1/question.txt new file mode 100644 index 0000000..a470f86 --- /dev/null +++ b/assignment1/question1/question.txt @@ -0,0 +1,4 @@ +Create a simple HTML file in notepad (do not use VSCode). To this, add some javascri +by: +a. Writing javascript between script tags +b. Writing javascript in a file and passing that as a src in the script tag. \ No newline at end of file diff --git a/assignment1/question2/JS_body_end.html b/assignment1/question2/JS_body_end.html new file mode 100644 index 0000000..fc97b90 --- /dev/null +++ b/assignment1/question2/JS_body_end.html @@ -0,0 +1,20 @@ + + + + + + + +

Hello world

+ + + + + +// when we try to access the element h1 in javascript (using document.getElementById) and change its content to “Goodbye World” +when the script is attached: +● at the end of body tag + +:- when the script is attached at the end of body tag then it change the content of the element h1. \ No newline at end of file diff --git a/assignment1/question2/JS_body_start.html b/assignment1/question2/JS_body_start.html new file mode 100644 index 0000000..107300d --- /dev/null +++ b/assignment1/question2/JS_body_start.html @@ -0,0 +1,22 @@ + + + + + + + + +

Hello world

+ + + + + + +// when we try to access the element h1 in javascript (using document.getElementById) and change its content to “Goodbye World” +when the script is attached: +● at the start of body tag + +:- when the script is attached at the start of body tag then it don't change the content of the element h1. \ No newline at end of file diff --git a/assignment1/question2/JS_in_headTag.html b/assignment1/question2/JS_in_headTag.html new file mode 100644 index 0000000..e5782ac --- /dev/null +++ b/assignment1/question2/JS_in_headTag.html @@ -0,0 +1,22 @@ + + + + + + + + + +

Hello world

+ + + + + +// when we try to access the element h1 in javascript (using document.getElementById) and change its content to “Goodbye World” +when the script is attached: +● in the head tag + +:- when the script is attached in head tag then it don't change the content of the element h1. \ No newline at end of file diff --git a/assignment1/question2/question.txt b/assignment1/question2/question.txt new file mode 100644 index 0000000..4ed1cd1 --- /dev/null +++ b/assignment1/question2/question.txt @@ -0,0 +1,14 @@ +2. Where to attach the script tag: + + + +

Hello World!

+ + + +Given the above html, find out what happens when you try to access the element h1 in +javascript (using document.getElementById) and change its content to “Goodbye World” +when the script is attached: +● in the head tag +● at the start of the body tag +● at the end of the body tag \ No newline at end of file diff --git a/assignment1/variables/ans1.js b/assignment1/variables/ans1.js new file mode 100644 index 0000000..8dc8bc5 --- /dev/null +++ b/assignment1/variables/ans1.js @@ -0,0 +1,14 @@ +//1. Declare a variable - let a;. On another line assign any value you like to a. Log the value +// of a before and after assignment. Try doing the same with const. + +let a; +console.log("log value of a before assigning:",a) +a="Josh"; +console.log("log a after assigning value in a :",a) + + + +// what we get after execution is as follow :- +//ans1.js +//log value of a before assigning: undefined +//log a after assigning value in a : Josh \ No newline at end of file diff --git a/assignment1/variables/ans2.js b/assignment1/variables/ans2.js new file mode 100644 index 0000000..7160eda --- /dev/null +++ b/assignment1/variables/ans2.js @@ -0,0 +1,49 @@ +//2. Use the typeof operator to find the types of different variables. Specially note what the +// typeof operator returns for arrays, null values and NaN. How can you find if a variable is +// an array or NaN besides typeof? + + +let a=5 +console.log("type of a is :",typeof a) + +let b='j' +console.log("type of b is :",typeof b) + +let arr=[1,2,3,4,5,6] +console.log("type of arr is :",typeof arr) + +let c='josh' +console.log("type of c is :",typeof c) + + + +let obj={ + key:"value", + 3:"value2", + "key1":"value3", + 4:889 +} +console.log("type of obj is :",typeof obj) + + +let d=null +console.log("type of d is : ",typeof d) + +let e=NaN +console.log("type of e is : ",typeof e) + + + +// the output of the above code is as following :- +// ans2.js +// type of a is : number +// type of b is : string +// type of arr is : object +// type of c is : string +// type of obj is : object +// type of d is : object +// type of e is : number + +// How can you find if a variable is an array or NaN besides typeof? +console.log(Array.isArray(arr)); //true +console.log(isNaN(e)); // true \ No newline at end of file diff --git a/assignment1/variables/ans3.js b/assignment1/variables/ans3.js new file mode 100644 index 0000000..d946f57 --- /dev/null +++ b/assignment1/variables/ans3.js @@ -0,0 +1,23 @@ +// Use let and const to create arrays and objects. Try modifying, deleting properties within +// the array or object. What do you expect to happen in both cases? +// What actually happens in both cases. What is the difference between an object declared +// as a let or a const variable? + +let arr=[1,2,3,4] + +arr[2]=22 +arr[0]=10 +console.log(arr) + +arr=["a","s","h","o"] +console.log(arr) +// As we see above we can change the elements of the arr and also we can reassign new array to arr in case of let it is possible but using const reassignment is not possible. +// using const + +const a=[1,2,3,4,5] +a[0]=10 // when we define array a using const then we can change the element of array but we can't reassign new array to a. +a[3]=33 +console.log(a) + +// a=["a","b","c"] // assignment to const variable is not possible +console.log(a)