Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions assignment1/Type_conversions/q1.js
Original file line number Diff line number Diff line change
@@ -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")
}
21 changes: 21 additions & 0 deletions assignment1/Type_conversions/q2.js
Original file line number Diff line number Diff line change
@@ -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;
}

}
9 changes: 9 additions & 0 deletions assignment1/Type_conversions/q3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let arraySize=25;

let arr=[]

for(let i=0; i<arraySize; i++){
arr[i]=i+1;
}
console.log(arr)

8 changes: 8 additions & 0 deletions assignment1/Type_conversions/q4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let arraySize=25;
let i=0;
let arr=[]
while(i<arraySize){
arr[i]=i+1;
i++
}
console.log(arr)
25 changes: 25 additions & 0 deletions assignment1/Type_conversions/q5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Can you use return instead of break in loops?

// yes we can use return instead of break in some cases when we want to exit from the function completely,
// in case of break we exit from the loop not from the function.

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
}
}
console.log("this will never printed after return statement executed.")
}

main();
main1();
17 changes: 17 additions & 0 deletions assignment1/Type_conversions/questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
1. Declare a variable let age = 25;. Write a series of if else statements that will:
○ Print child to the console if age is less than equal to 12.
○ Print teen to the console if age is between 13 and 18 (inclusive).
○ Print adult to the console if age is above 18


2. Do the same using switch case.


3. Declare a variable let arraySize = 25;. Using a for loop, add numbers from one
onwards into an array till the arraySize is reached.


4. Accomplish the same using a while loop.


5. Can you use return instead of break in loops?
17 changes: 17 additions & 0 deletions assignment1/arrays/mutatingVSnonmutating.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
question:-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

answer :- A mutating method modifies the original array, whereas a non-mutating method creates
and returns a new array without changing the original one.

mutating methods are : push, pop, sort because these methods modifies the original
array.

non-mutating methods are : filter, find, map these are non-mutating methods because these
methods returns new array.
74 changes: 74 additions & 0 deletions assignment1/arrays/q1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.
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"
}];

function filterByName(string){
const result=[]
for(const element of arr){
if(element.first_name.includes(string)){
result.push(element)
}
}

return result
}

let arr1=[]
arr1=filterByName("Town")
console.log("implemented function filterByName")
console.log(arr1)




58 changes: 58 additions & 0 deletions assignment1/arrays/q2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Use Array.map to return an array of all the email fields.
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("Array.map to return an array of all the email fields")
const emailArr=arr.map(element=>element.email);
console.log(emailArr)

61 changes: 61 additions & 0 deletions assignment1/arrays/q3.js
Original file line number Diff line number Diff line change
@@ -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)

67 changes: 67 additions & 0 deletions assignment1/arrays/q4.js
Original file line number Diff line number Diff line change
@@ -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)
Loading