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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/learn-js.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions quiz/manipulatedom.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="../css/index.css">
<script src="scripts/manipulatedom.js"></script>
</head>
<body>
<div id="root">
<p> This is a paragraph </p>
<button id="addTableBtn">Add Table </button>
</div>
<div id="root">
<p> This is a paragraph </p>
<button id="addTableBtn">Add Table </button>
</div>
</body>
</html>
6 changes: 3 additions & 3 deletions quiz/scripts/addEvent.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
42 changes: 23 additions & 19 deletions quiz/scripts/array1-q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
// 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();
}
};
};
8 changes: 5 additions & 3 deletions quiz/scripts/closure.js
Original file line number Diff line number Diff line change
@@ -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
};
23 changes: 17 additions & 6 deletions quiz/scripts/hideEmail-q.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
7 changes: 7 additions & 0 deletions quiz/scripts/logger.js
Original file line number Diff line number Diff line change
@@ -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.');
}
}
5 changes: 3 additions & 2 deletions quiz/scripts/manipulatedom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function createTRNode(colNodes) {
let trNode = document.createElement("tr");
colNodes.forEach(function(colNode) {
trNode.appendChild(colNode);
})
});
return trNode;
}

Expand All @@ -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);
}
35 changes: 19 additions & 16 deletions quiz/scripts/map-deep.js
Original file line number Diff line number Diff line change
@@ -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);

[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);
21 changes: 13 additions & 8 deletions quiz/scripts/stack-q1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class PStack {
showId() {
return this.#id;
}

}

class PStackImpl extends PStack {
Expand All @@ -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 }]
36 changes: 18 additions & 18 deletions quiz/vars1.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<html>
<head>
<script>
// Fix the error in the code below
let x = 1;
var y = 2;
{
y = y + 1;
console.log(x+y);
let y = 2;
}
console.log(x);
console.log(y);
<head>
<script>
// Fix the error in the code below
let x = 1;
var y = 2;
{
let y = 2; // Declare y within the block scope
y = y + 1; // Increment the value of y
console.log(x + y); // Print the sum of x and y
}
console.log(x); // Print the value of x
console.log(y); // Print the value of y (global scope)

</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
27 changes: 14 additions & 13 deletions quiz/vars2.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<html>
<head>
<script>
// change the script such that i=0,1,2,3,4 is logged in the console
for (var i = 0; i < 5; i++) {
// setTimeout executes the function after 5 ms
<head>
<script>
for (var i = 0; i < 5; i++) {
// Using IIFE (Immediately Invoked Function Expression) to capture the current value of i
(function(index) {
setTimeout(function() {
console.log(i);
console.log(index);
}, 0);
}
</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
})(i); // Pass the current value of i as an argument to the IIFE
}
</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
35 changes: 19 additions & 16 deletions quiz/vars3.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<html>
<head>
<script>
// Fix all errors in the code below
const PI = 3.14159;
{
let radius = 5;
let area = PI * radius * radius;
PI = 3.0;
}
console.log(area);
<head>
<script>
// Fix all errors in the code below
const PI = 3.14159; // Define PI as a constant
let area; // Declare area outside the block scope

</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
{
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

</script>
</head>
<body>
<h1> A Web Page </h1>
</body>
</html>
Loading