const original = {
name: 'Sarah',
scores: [85, 92, 78],
details: {
age: 25,
city: 'Portland'
}
};
const copy1 = { ...original };
const copy2 = JSON.parse(JSON.stringify(original));
copy1.name = 'Emma';
copy1.scores.push(95);
copy1.details.age = 30;
console.log(original.name, original.scores.length, original.details.age);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const firstArrayData = [ 'JavaScript', 'Universe' ];
const secondArrayData = [ 'JavaScript', 'Universe' ];
console.log(firstArrayData == secondArrayData);
console.log(firstArrayData === secondArrayData);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const userInput = "<script>alert('xss')</script>";
const sanitized = userInput.replace(/<script[^>]*>.*?<\/script>/gi, '');
const users = new Map();
users.set('admin', { password: 'secret123', role: 'admin' });
users.set('guest', { password: 'guest', role: 'user' });
function authenticate(username, password) {
const user = users.get(username);
return user && user.password === password ? user.role : null;
}
const role1 = authenticate('admin', 'secret123');
const role2 = authenticate('guest', 'wrong');
const role3 = authenticate('hacker', 'secret123');
console.log(sanitized);
console.log(role1, role2, role3);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const a = { value: 1 };
const b = Object.create(a);
b.value = 2;
console.log(b.value);
console.log(a.value);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const matrix = [
[2, 4],
[6, 8],
];
const result = matrix.reduceRight((acc, row) => acc.concat(row.map(num => num * 2)), []);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3, 4, 5];
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * 2)
.reduce((acc, n) => acc + n, 0);
const original = numbers.slice();
original.reverse();
const flattened = [[1, 2], [3], [4, 5]].flat();
const found = flattened.find(n => n > 3);
console.log(result);
console.log(original.length);
console.log(found);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function main({ x, y } = { x: 1, y: 2 }) {
console.log(x, y);
}
main({ x: 5 });
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
try {
const obj = null;
obj.property = 'value';
} catch (e) {
console.log(e.name);
}
try {
undeclaredVariable;
} catch (e) {
console.log(e.name);
}
try {
JSON.parse('invalid json');
} catch (e) {
console.log(e.name);
}
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
obj[prop] = value.toString().toUpperCase();
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'boston';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function Person(name) {
this.name = name;
this.sayName = () => console.log(this.name);
}
const person1 = new Person('David');
const person2 = { name: 'Not David', sayName: person1.sayName };
person2.sayName();
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function greet(name) {
return `Hello, ${name}!`;
}
function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
return result + str + (values[i] ? `<em>${values[i]}</em>` : '');
}, '');
}
const user = 'Sarah';
const status = 'online';
console.log(highlight`User ${user} is currently ${status}.`);
...Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
async function foo() {
console.log('Start');
await Promise.resolve().then(() => {
console.log('Inside Promise');
});
console.log('End');
}
foo();
console.log('Outside');
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Maya', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
if (typeof value === 'string') {
obj[prop] = value.toUpperCase();
} else {
obj[prop] = value;
}
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'tokyo';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const result = (function() {
let count = 0;
return {
increment() {
return ++count;
},
get value() {
return count;
},
reset() {
const oldCount = count;
count = 0;
return oldCount;
}
};
})();
result.increment();
result.increment();
console.log(result.reset() + result.value + result.increment());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const user = {
profile: {
settings: {
theme: 'dark'
}
}
};
const getTheme = (obj) => obj?.profile?.settings?.theme ?? 'light';
const getLanguage = (obj) => obj?.profile?.settings?.language ?? 'en';
const getNotifications = (obj) => obj?.profile?.notifications?.enabled ?? true;
console.log(getTheme(user));
console.log(getLanguage(user));
console.log(getNotifications(user));
console.log(getTheme(null));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
let obj = { a: 1 };
let proto = { b: 2 };
Object.setPrototypeOf(obj, proto);
for (let key in obj) {
console.log(key);
}
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const weakMap = new WeakMap();
const array = [{}, {}];
array.forEach(obj => weakMap.set(obj, obj));
const result = array.map(obj => weakMap.get(obj) === obj);
console.log(result);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const team = {
members: ['Alice', 'Bob', 'Charlie'],
[Symbol.iterator]: function*() {
let index = 0;
while(index < this.members.length) {
yield this.members[index++].toUpperCase();
}
}
};
const result = [];
for (const member of team) {
result.push(member);
}
console.log(result.join('-'));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var str="My name is John";
var words1=str.split(" ",3);
console.log("words1:",words1);
var words2=str.split(" ",5);
console.log("words2:",words2);
Ответ:
words1:[ 'My', 'name', 'is' ]
words2:[ 'My', 'name', 'is', 'John' ]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function createCounter() {
let count = 0;
return {
increment: () => ++count,
decrement: () => --count,
getValue: () => count
};
}
const counter1 = createCounter();
const counter2 = createCounter();
counter1.increment();
counter1.increment();
counter2.increment();
console.log(counter1.getValue(), counter2.getValue());
counter1.decrement();
console.log(counter1.getValue(), counter2.getValue());
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM