From c104563e3309f0281ef50399e2da6ef26b80ae14 Mon Sep 17 00:00:00 2001 From: Ankur Haritosh Date: Fri, 2 Feb 2024 23:51:18 -0500 Subject: [PATCH] fixing files --- .idea/.gitignore | 3 +++ .idea/learn-js.iml | 9 ++++++++ .idea/misc.xml | 6 +++++ .idea/modules.xml | 8 +++++++ .idea/vcs.xml | 6 +++++ quiz/manipulatedom.html | 12 +++++----- quiz/scripts/addEvent.js | 6 ++--- quiz/scripts/array1-q.js | 42 +++++++++++++++++++---------------- quiz/scripts/closure.js | 8 ++++--- quiz/scripts/hideEmail-q.js | 23 ++++++++++++++----- quiz/scripts/logger.js | 7 ++++++ quiz/scripts/manipulatedom.js | 5 +++-- quiz/scripts/map-deep.js | 35 ++++++++++++++++------------- quiz/scripts/stack-q1.js | 21 +++++++++++------- quiz/vars1.html | 36 +++++++++++++++--------------- quiz/vars2.html | 27 +++++++++++----------- quiz/vars3.html | 35 ++++++++++++++++------------- quiz/where2putjs.html | 17 ++++++++------ 18 files changed, 190 insertions(+), 116 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/learn-js.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/learn-js.iml b/.idea/learn-js.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/learn-js.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..e0844bc7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..c4805284 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/quiz/manipulatedom.html b/quiz/manipulatedom.html index 653842f6..04ca4fcf 100644 --- a/quiz/manipulatedom.html +++ b/quiz/manipulatedom.html @@ -1,11 +1,13 @@ - + + -
-

This is a paragraph

- -
+
+

This is a paragraph

+ +
+ diff --git a/quiz/scripts/addEvent.js b/quiz/scripts/addEvent.js index 53ed60e8..0e9d738e 100644 --- a/quiz/scripts/addEvent.js +++ b/quiz/scripts/addEvent.js @@ -1,8 +1,8 @@ window.onload = function() { let x = document.getElementById('myBtn'); - x.addEventListener('mouseover', myFunction('Moused over!')); - x.addEventListener('click', mySecondFunction('Clicked!')); - x.addEventListener('mouseout', myThirdFunction('Moused out!')); + x.addEventListener('mouseover', function() { myFunction('Moused over!') }); + x.addEventListener('click', function() { mySecondFunction('Clicked!') }); + x.addEventListener('mouseout', function() { myThirdFunction('Moused out!') }); } function myFunction(msg) { diff --git a/quiz/scripts/array1-q.js b/quiz/scripts/array1-q.js index a5b8e944..ac9716fc 100644 --- a/quiz/scripts/array1-q.js +++ b/quiz/scripts/array1-q.js @@ -8,25 +8,29 @@ window.onload = function() { const MAX_HISTORY = 5; btn.onclick = () => { - // we will only allow a term to be entered if the search input isn't empty - if (inp.value !== '') { - // empty the list so that we don't display duplicate entries - // the display is regenerated every time a search term is entered. - list.innerHTML = ''; + // We will only allow a term to be entered if the search input isn't empty + if (inp.value !== '') { + // Add the search term to the history array + myHistory.push(inp.value); - // loop through the sorted array, and display all the search terms in the list - for (const itemText of myHistoryCopy) { - - } + // Empty the list so that we don't display duplicate entries + list.innerHTML = ''; - // If the array length is 5 or more, remove the oldest search term - if (myHistory.length >= MAX_HISTORY) { - - } + // Loop through the history array and display all the search terms in the list + for (const itemText of myHistory) { + const li = document.createElement('li'); + li.textContent = itemText; + list.appendChild(li); + } - // empty the search input and focus it, ready for the next term to be entered - inp.value = ''; - btn.focus(); - } - } -} \ No newline at end of file + // If the array length is greater than the maximum history length, remove the oldest search term + if (myHistory.length > MAX_HISTORY) { + myHistory.shift(); // Remove the oldest search term from the beginning of the array + } + + // Empty the search input and focus it, ready for the next term to be entered + inp.value = ''; + btn.focus(); + } + }; +}; diff --git a/quiz/scripts/closure.js b/quiz/scripts/closure.js index 627414d2..caa7ef19 100644 --- a/quiz/scripts/closure.js +++ b/quiz/scripts/closure.js @@ -1,13 +1,15 @@ function setColor(set) { + return function() { let changeColor = set; - if(changeColor) { + if (changeColor) { let userColor = document.getElementById('color').value; document.getElementById('myPara').style.color = userColor; } - + }; } window.onload = function() { let toggle = true; document.getElementById('btn').onclick = setColor(toggle); -} + toggle = !toggle; // Toggle the value of toggle for the next click +}; diff --git a/quiz/scripts/hideEmail-q.js b/quiz/scripts/hideEmail-q.js index ba41173b..d4bf84bc 100644 --- a/quiz/scripts/hideEmail-q.js +++ b/quiz/scripts/hideEmail-q.js @@ -1,11 +1,22 @@ -/* anonymizes the username(local part) of an email ID, that is, -replace the username part with * characters. -*/ const emails = ['mary@northeastern.edu', 'ari@khoury.northeastern.edu', 'jk_neu.edu', 'jk@neu.edu', 'jsned@', 'ai_me@mugar.northeastern.edu']; + window.onload = function hideEmail() { - const list = document.getElementById('emails') + const list = document.getElementById('emails'); list.innerHTML = ''; + for (const email of emails) { - // complete the loop + let anonymizedEmail; + if (email.includes('@')) { + const atIndex = email.indexOf('@'); + const localPart = email.slice(0, atIndex); + const domainPart = email.slice(atIndex); + anonymizedEmail = '*'.repeat(localPart.length) + domainPart; + } else { + anonymizedEmail = email; // Handle email addresses without domain parts + } + + const li = document.createElement('li'); + li.textContent = anonymizedEmail; + list.appendChild(li); } -} \ No newline at end of file +}; diff --git a/quiz/scripts/logger.js b/quiz/scripts/logger.js index 5d03eb67..e699ec80 100644 --- a/quiz/scripts/logger.js +++ b/quiz/scripts/logger.js @@ -1 +1,8 @@ // Define a JavaScript function called logMsg() that can be used to log an error message for any object that contains the property errMsg. +function logMsg(obj) { + if (obj && obj.errMsg) { + console.error(obj.errMsg); + } else { + console.error('Error: The object does not contain errMsg property.'); + } +} diff --git a/quiz/scripts/manipulatedom.js b/quiz/scripts/manipulatedom.js index 32e731e1..7eba3eca 100644 --- a/quiz/scripts/manipulatedom.js +++ b/quiz/scripts/manipulatedom.js @@ -6,7 +6,7 @@ function createTRNode(colNodes) { let trNode = document.createElement("tr"); colNodes.forEach(function(colNode) { trNode.appendChild(colNode); - }) + }); return trNode; } @@ -25,7 +25,8 @@ function addTable() { const tableNode = document.createElement("table"); for(let i = 0; i < 3; i++) { let col1 = createTDNode(createTxtNode("Cell (" + i + ", 0)")); - tableNode.appendChild(createTRNode([col1])); + let row = createTRNode([col1]); + tableNode.appendChild(row); } document.getElementById("root").appendChild(tableNode); } diff --git a/quiz/scripts/map-deep.js b/quiz/scripts/map-deep.js index e97665e5..ccc1fb69 100644 --- a/quiz/scripts/map-deep.js +++ b/quiz/scripts/map-deep.js @@ -1,18 +1,21 @@ // Original matrix (list of lists) const originalMatrix = [ - [3, 2, 1], - [4, 5, 6], - [7, 9, 8] - ]; - - const newMatrix = originalMatrix.map((row) => { - return row.sort(); - }); - - // Modifying the original matrix (changing the last element of the first row) - originalMatrix[0][2] = 99; - - // Displaying the original and new matrices - console.log('Original Matrix (modified):', originalMatrix); - console.log('New Matrix (unaffected):', newMatrix); - \ No newline at end of file + [3, 2, 1], + [4, 5, 6], + [7, 9, 8] +]; + +// Create a deep copy of originalMatrix +const deepCopyMatrix = JSON.parse(JSON.stringify(originalMatrix)); + +const newMatrix = originalMatrix.map((row) => { + return row.sort(); +}); + +// Modifying the original matrix (changing the last element of the first row) +originalMatrix[0][2] = 99; + +// Displaying the original and new matrices +console.log('Original Matrix (modified):', originalMatrix); +console.log('New Matrix (unaffected):', newMatrix); +console.log('Deep Copy Matrix:', deepCopyMatrix); diff --git a/quiz/scripts/stack-q1.js b/quiz/scripts/stack-q1.js index 0d719223..8e7040f6 100644 --- a/quiz/scripts/stack-q1.js +++ b/quiz/scripts/stack-q1.js @@ -8,7 +8,6 @@ class PStack { showId() { return this.#id; } - } class PStackImpl extends PStack { @@ -17,17 +16,23 @@ class PStackImpl extends PStack { } push(p) { - return this._persons.push(p) + this._persons.push(p); // Corrected: Push the person object into the _persons array } pop() { - return this._persons.pop().age + const person = this._persons.pop(); // Corrected: Pop the person object from the _persons array + return person ? person.age : undefined; // Corrected: Return the age of the popped person object if it exists + } + + get persons() { + return this._persons; // Getter method to access the _persons array } } let pstack = new PStackImpl(); -pstack.persons = [{name: 'Jojo', age: 21}, {name: 'Gabi', age: 29}] -pstack.push({name: 'Dein', age: 19}); -console.log(pstack.pop()); -console.log(pstack.pop()); -console.log(pstack.persons); +pstack.push({ name: 'Jojo', age: 21 }); +pstack.push({ name: 'Gabi', age: 29 }); +pstack.push({ name: 'Dein', age: 19 }); +console.log(pstack.pop()); // Output: 19 +console.log(pstack.pop()); // Output: 29 +console.log(pstack.persons); // Output: [{ name: 'Jojo', age: 21 }] diff --git a/quiz/vars1.html b/quiz/vars1.html index 3e4b0236..e0fd3d8a 100644 --- a/quiz/vars1.html +++ b/quiz/vars1.html @@ -1,20 +1,20 @@ - - - - -

A Web Page

- - \ No newline at end of file + + + +

A Web Page

+ + diff --git a/quiz/vars2.html b/quiz/vars2.html index 59895a85..50e09fcf 100644 --- a/quiz/vars2.html +++ b/quiz/vars2.html @@ -1,16 +1,17 @@ - - - - -

A Web Page

- - \ No newline at end of file + })(i); // Pass the current value of i as an argument to the IIFE + } + + + +

A Web Page

+ + diff --git a/quiz/vars3.html b/quiz/vars3.html index 5b2d10dd..b0c205ce 100644 --- a/quiz/vars3.html +++ b/quiz/vars3.html @@ -1,18 +1,21 @@ - - - - -

A Web Page

- - \ No newline at end of file + { + let radius = 5; + area = PI * radius * radius; // Assign value to area + // Cannot reassign a value to a constant + // PI = 3.0; // Remove this line + } + console.log(area); // Log the value of area + + + + +

A Web Page

+ + diff --git a/quiz/where2putjs.html b/quiz/where2putjs.html index 0dff0b61..64d41f28 100644 --- a/quiz/where2putjs.html +++ b/quiz/where2putjs.html @@ -4,12 +4,6 @@ Title -
@@ -18,5 +12,14 @@

Demo JavaScript in Body

+ - \ No newline at end of file +