0% found this document useful (0 votes)
9 views

Objects Practice

Uploaded by

oughbra476
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Objects Practice

Uploaded by

oughbra476
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Objects ‫األسئلة مع اإلجابات لتدريب الكائنات‬

/*
1- *** Compare Objects ***
Define a function, compareObjects, that accepts two objects as
arguments.

compareObjects should return true if both objects have exactly the same
key/value pairs. Otherwise, compareObjects should return false.
Assume the objects are not nested.

compareObjects({ name: "Yara" }, { name: "Yara" }) // -> true


compareObjects({ name: "Omar" }, { name: "Omar", age: 12 }) // -> false
*/
function compareObjects(object1, object2) {
if (Object.keys(object1).length !== Object.keys(object2).length) {
return false;
}
for (key in object1) {
if (object1[key] !== object2[key]) {
return false;
}
}
return true;
}
console.log(compareObjects({ name: “Yara" }, { name: "Yara" }));
/* 2- *** Attendance Check ***
Define a function, attendanceCheck, that accepts a day of the week
as a string.
Using the globally-defined classRoom array, attendanceCheck should
return a new array with only the names of the students present
on the inputted day of the week.
classCheck('Monday'); // => ['Rania', 'Sara']
classCheck('Wednesday'); // => ['Rania', 'Lina']
*/
let classRoom = [
{
Rania: [
{ Sunday: true },
{ Monday: true },
{ Tuesday: true },
{ Wednesday: true },
{ Thursday: true },
],
},
{
Lina: [
{ Sunday: true },
{ Monday: false },
{ Tuesday: false },
{ Wednesday: true },
{ Thursday: false },
],
},
{
Sara: [
{ Sunday: false },
{ Monday: true },
{ Tuesday: true },
{ Wednesday: false },
{ Thursday: true },
],
},
{
Amira: [
{ Sunday: true },
{ Monday: false },
{ Tuesday: false },
{ Wednesday: false },
{ Thursday: false },
],
},];
function attendanceCheck(day) {
let output = [];
for (let i = 0; i < classRoom.length; i++) {
let obj = classRoom[i];
let studentName = Object.keys(obj)[0];
let attendanceArray = obj[studentName];
for (let j = 0; j < attendanceArray.length; j++) {
let dayAttendance = attendanceArray[j];
if (dayAttendance[day]) {
output.push(studentName);
}
}
}
return output;
}

console.log(classCheck('Monday')); // => ['Rania', 'Sara']


/*
3- *** Call Them All ***
Write a function callThemAll, that, given an object and a value,
calls every method in the object, passing in the value as the
argument with each call.

callThemAll should return an array with all of the returned values


from each method invocation. The order of the elements in the
returned array does not matter.

callThemAll(addNums, 100); // => [110, 120]

*/

function callThemAll(object, value) {


let output = [];
for (key in sumNum) {
let kValue = sumNum[key];
if (typeof kValue === "function") {
let returnValue = sumNum[key](value);
output.push(returnValue);
}
}
return output;
}

console.log(callThemAll(sumNum, 100)); // => [110, 120]

You might also like