[Day 9]

Task : UpperCase

Write function makeUpperCase.

Solution :

function makeUpperCase(str) {
return(str.toUpperCase()); //
}
const a = String.prototype.toUpperCase.call({
toString: function toString() {
return 'abcdef';
}
});

const b = String.prototype.toUpperCase.call(true);

// prints out 'ABCDEF TRUE'.
console.log(a, b);

Task : Even or Odd

Create a function (or write a script in Shell) that takes an integer as an argument and returns “Even” for even numbers or “Odd” for odd numbers.

Solution :

function even_or_odd(number) {
return (number % 2) ? "Odd" : "Even";
}
  • What to Know : Conditional (ternary) operator
A simple example:
var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"

One common usage is to handle a value that may be null:

function greeting(person) {
var name = person ? person.name : "stranger";
return "Howdy, " + name;
}

console.log(greeting({name: 'Alice'})); // "Howdy, Alice"
console.log(greeting(null)); // "Howdy, stranger"​​​​​

Task : Odd or Even?

Given an array of numbers (a list in groovy), determine whether the sum of all of the numbers is odd or even.
  • Give your answer in string format as ‘odd’ or ‘even’.
  • If the input array is empty consider it as: [0] (array with a zero).

Example :

oddOrEven([0]) returns "even"
oddOrEven([2, 5, 34, 6]) returns "odd"
oddOrEven([0, -1, -5]) returns "even"

Solution :

function oddOrEven(array) {
} 

What to Know More : Understand Negative Integer