DEV Community

Randy Rivera
Randy Rivera

Posted on

Truthy & Algorithm Scripting and Data Structures: Part 5

  • 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");
Enter fullscreen mode Exit fullscreen mode
  • 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;
Enter fullscreen mode Exit fullscreen mode
  • Or
function truthCheck(collection, pre) {
  return collection.every(function(elem) {
    return elem[pre]
  })
}
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

// 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)
Enter fullscreen mode Exit fullscreen mode
  • 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();
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)