Skip to content

Instantly share code, notes, and snippets.

@lmcarreiro
Last active April 7, 2021 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmcarreiro/015a1b34bd22df2c2a5f89aa0c93df3f to your computer and use it in GitHub Desktop.
Save lmcarreiro/015a1b34bd22df2c2a5f89aa0c93df3f to your computer and use it in GitHub Desktop.
Javascript questions
/* eslint-disable */
function q1() {
const person = { age: 20, name: "Ben" };
const peopleArray = [person, person, person];
peopleArray[1].name = "Joe";
console.log(peopleArray[0].name, peopleArray[1].name, peopleArray[2].name);
}
function q2() {
const person1 = {
name: "Willie",
address: {
city: "Austin",
state: "Texas",
},
};
const person2 = { ...person1 };
person2.name = "Waylon";
person2.address.city = "Fort Worth";
console.log(person1.name);
console.log(person1.address.city, person1.address.state);
console.log(person2.name);
console.log(person2.address.city, person2.address.state);
}
function q3() {
var max = 10;
var objects = {};
for (var index = 1; index <= max; index++) {
fetch(`/item/${index}`).then(data => {
objects[index] = data;
});
}
return objects;
}
function q4() {
var max = 10;
var objects = {};
var count = 0;
for (var index = 1; index <= max; index++) {
fetch(`/item/${index}`).then(data => {
objects[index] = data;
count++;
if (count === 10) {
showResults(objects);
}
});
}
}
function q5() {
// convert to async/await
fetch("/users")
.then(users => {
return Promise.all(
users.map(u =>
fetch(`/user/${u.id}/isManager`).then(({ isManager }) => ({ id: u.id, isManager: isManager ? "yes" : "no" })),
),
).then(users => {
return users.filter(u => u.isManager === "yes").map(u => u.id);
});
})
.catch(err => console.error("Error fetching users:", err))
.then(managersIds => doSomething(managersIds));
}
function q6() {
function Test() {
this.counter = 1;
this.increment = function () {
this.counter++;
};
}
const t = new Test();
t.increment();
setTimeout(t.increment, 100);
setTimeout(() => console.log(t.counter), 200);
}
function q7() {
let test = 1;
let x = 10;
setTimeout(() => test++, 1);
for (let i = 0; i < 100_000_000; i++) {
x = Math.random() > 0.5 ? Math.pow(x, Math.random()) : Math.log10(x);
}
console.log(test);
}
// q8: True or False? In the code below, myColor is initialized with a boolean value of true.
const myColor1 = "" || "Purple" || "Red";
// q9: True or False? In the code below, myColor is initialized with a boolean value of true.
const myColor2 = "" && "Purple" && "Red";
// q10: Name a JavaScript String function that modifies the original string
// ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment