Hello everyone, today we have an interesting challenge where we need to check if the second argument is truthy on all elements of the first argument. Basically if we're given an array collection of objects. The second arguments pre will be an object property and you need to return true if its value is truthy otherwise, return false.
In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.
Code:
function truthCheck(collection, pre) {
return pre;
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
- Answer:
function truthCheck(collection, pre) {
for (let i = 0; i < collection.length; i++) {
if (!collection[i][pre]) {
return false;
}
}
return true
}
console.log(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")); // will display true;
- Or
function truthCheck(collection, pre) {
return collection.every(function(elem) {
return elem[pre]
})
}
- The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
- Answer:
function addTogether() {
let args = Array.from(arguments)
if (args.some(elem => typeof elem !== "number")) {
return undefined;
} else if (args.length === 1) {
return x => addTogether(args[0], x)
} else {
return args[0] + args[1];
}
}
console.log(addTogether(2,3)); // will display 5
console.log(addTogether(5)(7)); // will display 12
// The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
Using and Filling in the object constructor to make a person.
- Today we're gonna be using the following methods below
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)
- For this particular challenge. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.
- Code:
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
return "";
};
return firstAndLast;
};
var bob = new Person('Bob Ross');
bob.getFullName();
- Answer:
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFirstName = function() {
return firstAndLast.split(" ")[0];
};
this.getLastName = function() {
return firstAndLast.split(" ")[1]
}
this.getFullName = function() {
return firstAndLast;
}
this.setFirstName = function(newFirstName) {
return firstAndLast = newFirstName + " " + this.getLastName()
}
this.setLastName = function(newLastName) {
return firstAndLast = this.getFirstName() + " " + newLastName
}
this.setFullName = function(newFullName) {
return firstAndLast = newFullName;
}
}
var bob = new Person('Randy Rivera');
console.log(bob.getFirstName()); // Randy
console.log(bob.getLastName()); // Rivera
console.log(bob.getFullName()); // Randy Rivera
console.log(bob.setFirstName("Anakin")); // Anakin Rivera
console.log(bob.setLastName("Hernandez")); // Anakin Hernandez bob.getFullName() should return the string Anakin Hernandez after bob.setLastName("Hernandez").
console.log(bob.setFullName("Anakin Hernandez")) // Anakin Hernandez
Top comments (0)