JSchallenger にチャレンジ - Fundamentals / Objects
Notion にまとめていたものを少しずつ移行中。後ほど解説も追加します。
Fundamentals 編
JSchallenger にチャレンジ - Fundamentals / Basics
JSchallenger にチャレンジ - Fundamentals / Arrays
JSchallenger にチャレンジ - Fundamentals / Objects
JSchallenger にチャレンジ - Fundamentals / Dates
JSchallenger にチャレンジ - Fundamentals / Sets
DOM 編
JSchallenger にチャレンジ - DOM / DOM selector methods
JSchallenger にチャレンジ - DOM / Events and user interactions
JSchallenger にチャレンジ - DOM / DOM manipulation
JSchallenger にチャレンジ - DOM / DOM fundamentals
JSchallenger にチャレンジ - DOM / Recursive functions
Accessing object properties one
Write a function that takes an object with two properties as argument
It should return the value of the property with key country
回答
function myFunction(obj) {
return obj.country
}
Accessing object properties two
Write a function that takes an object with two properties as argument
It should return the value of the property with key 'prop-2'
Tipp: you might want to use the square brackets property accessor
回答
function myFunction(obj) {
return obj['prop-2']
}
Accessing object properties three
Write a function that takes an object with two properties and a string as arguments
It should return the value of the property with key equal to the value of the string
回答
function myFunction(obj, key) {
return obj[key]
}
Check if property exists in object
Write a function that takes an object (a) and a string (b) as argument
Return true if a has a property with key b
Return false otherwise
回答
function myFunction(a, b) {
return b in a;
}
Creating Javascript objects one
Write a function that a string (a) as argument
Create an object that has a property with key 'key' and a value of a
Return the object
回答
function myFunction(a) {
return { key: a };
}
Creating Javascript objects two
Write a function that takes two strings (a and b) as arguments
Create an object that has a property with key 'a' and a value of 'b'
Return the object
回答
function myFunction(a, b) {
return { [a]: b };
}
Creating Javascript objects three
Write a function that takes two arrays (a and b) as arguments
Create an object that has properties with keys 'a' and corresponding values 'b'
Return the object
回答
function myFunction(a, b) {
return a.reduce((prev, current, index) => {
return {
...prev,
[current]: b[index]
}
},{})
}
Extract keys from Javascript object
Write a function that takes an object (a) as argument
Return an array with all object keys
function myFunction(a) {
return Object.keys(a);
}
Sum object values
Write a function that takes an object (a) as argument
Return the sum of all object values
回答
function myFunction(a) {
return Object.values(a).reduce((prev, current) => {
return prev + current;
},0)
}
Remove a property from an object
Write a function that takes an object as argument
It should return an object with all original object properties
except for the property with key 'b'
回答
function myFunction(obj) {
const {b, ...param} = obj;
return param;
}
Merge two objects with matching keys
Write a function that takes two objects as arguments
Unfortunately, the property 'b' in the second object has the wrong key
It should be named 'd' instead
Merge both objects and correct the wrong property name
Return the resulting object
It should have the properties 'a', 'b', 'c', 'd', and 'e'
回答
function myFunction(x, y) {
const { b, ...param } = y;
return { ...x, ...param, d: b };
}
Multiply all object values by x
Write a function that takes an object (a) and a number (b) as arguments
Multiply all values of 'a' by 'b'
Return the resulting object
回答
function myFunction(a, b) {
return Object.entries(a).reduce((prev, current) => {
const [key, value] = [...current];
return {
...prev,
[key]: value * b,
};
}, {});
}
Swap object keys and values
Write a function that takes an object as argument
Somehow, the properties and keys of the object got mixed up
Swap the Javascript object's key with its values and return the resulting object
回答
function myFunction(obj) {
return Object.entries(obj).reduce((prev, current) => {
const [key, value] = [...current];
return {
...prev,
[value]: key,
};
}, {});
}
Replace empty strings in object with null values
Write a function that takes an object as argument
Some of the property values contain empty strings
Replace empty strings and strings that contain only whitespace with null values
Return the resulting object
回答
function myFunction(obj) {
return Object.entries(obj).reduce((prev, current) => {
const [key, value] = [...current];
return {
...prev,
[key]: value.trim() === '' ? null : value,
};
}, {});
}
Extracting information from objects
Write a function that takes an object as argument containing properties with personal information
Extract firstName, lastName, size, and weight if available
If size or weight is given transform the value to a string
Attach the unit cm to the size
Attach the unit kg to the weight
Return a new object with all available properties that we are interested in
回答
function myFunction(obj) {
return {
fn: obj.fn,
ln: obj.ln,
...(obj.size && { size: `${obj.size}cm` }),
...(obj.weight && { weight: `${obj.weight}kg` }),
};
}
Add property to each object in array
Write a function that takes an array of objects and a string as arguments
Add a property with key 'continent' and value equal to the string to each of the objects
Return the new array of objects
Tipp: try not to mutate the original array
回答
function myFunction(arr, str) {
return arr.map((obj) => ({ ...obj, continent: str }));
}
Convert array to object with counter
Write a function that takes an array of numbers as argument
Convert the array to an object
It should have a key for each unique value of the array
The corresponding object value should be the number of times the key occurs within the array
回答
function myFunction(a) {
return a.reduce((prev, current) => {
return {
...prev,
[current]: (prev[current] ?? 0) + 1,
};
}, {});
}
Check if property exists in object and is truthy
Write a function that takes an object (a) and a string (b) as argument
Return true if the object has a property with key 'b', but only if it has a truthy value
In other words, it should not be null or undefined or false
Return false otherwise
回答
function myFunction(a, b) {
return !!a[b]
}
Return nested object property
Write a function that takes an object as argument
In some cases the object contains other objects with some deeply nested properties
Return the property 'b' of object 'a' inside the original object if it exists
If not, return undefined
回答
function myFunction(obj) {
return obj.a?.b;
}