In: Computer Science
I need some asistance understanding of what to do for this problem in javascript:
* Problem 9 - find the largest number in the list of arguments
*
* Allow any number of arguments to be passed to the function. Allow both
* String and Number arguments to be passed, but throw an error if any other
* type is passed to the function (e.g., Boolean, Date, etc.). If the list
* is empty (nothing passed to the function), return null. Otherwise, return
* the largest value that was passed to the function as an argument:
*
* findLargest(1, 2, '3', '4', 5) should return 5
* findLargest('5') should also return 5
* findLargest(5, 3, 2, 1) should also return 5
******************************************************************************/
function findLargest() {
// Your code here...
}
here are the test cases:
const { findLargest } = require('./solutions');
describe('Problem 9 - findLargest', function() {
test('should find the largest number in a list', function() {
let largest = findLargest(1, 2, 3);
expect(largest).toBe(3);
});
test('should find the largest number in a list of 1', function() {
const largest = findLargest(1);
expect(largest).toBe(1);
});
test('should find the largest number in a long list', function() {
// https://github.com/gromgit/jstips-xe/blob/master/tips/33.md
const list = Array.apply(null, { length: 5000 }).map(Function.call, Number);
const largest = findLargest.apply(null, list);
expect(largest).toBe(4999);
});
test('should work with negative numbers', function() {
const largest = findLargest(1, 2, 3, -1, -2, -3);
expect(largest).toBe(3);
});
test('should work with strings that are numbers', function() {
const largest = findLargest('1', '2', '3');
expect(largest).toBe(3);
});
test('should work with decimals', function() {
const largest = findLargest(0.01, 0.001);
expect(largest).toBe(0.01);
});
test('should throw if a Boolean is included in the list', function() {
function shouldThrow() {
findLargest(1, true, 3);
}
expect(shouldThrow).toThrow();
});
test('should throw if an Object is included in the list', function() {
function shouldThrow() {
findLargest(1, console, 3);
}
expect(shouldThrow).toThrow();
});
test('should throw if a null is included in the list', function() {
function shouldThrow() {
findLargest(1, null, 3);
}
expect(shouldThrow).toThrow();
});
test('should throw if a undefined is included in the list', function() {
function shouldThrow() {
findLargest(1, undefined, 3);
}
expect(shouldThrow).toThrow();
});
test('should return null if the list is empty', function() {
const largest = findLargest();
expect(largest).toBe(null);
});
});
Below Is the javascript code for above problem this fullfill all test cases mentioned above
Hope you like the solution , please give a thumbs up if you did like :)
// here i use rest parameter syntax to define the function to pass any number of args.
function findLargest(...args) {
  if(arguments.length === 0) {
        return null
  } else {
        try{
            // "arguments" us a type of variable in javascript which keep track of all arguments passed
            // this is treated as an array 
            // if args is just a list object then this condition will be true
                if(arguments.length == 1 && typeof arguments[0] == "object") {
            
            for (var i = 0; i < arguments[0].length; i++) {
                // checking passed elements are of type number or string else throw type error
                        if(typeof arguments[0][i] == "number" || typeof arguments[0][i] == "string") {
                                arguments[0][i] = Number(arguments[0][i])
                        }
                        else {
                    // throwing type error
                                throw new TypeError('Unexpected type found') 
                        }
            
                        }
                // Math.max does not work on object type variable so below syntax is used
                return Math.max.apply(Math, ...args)
            } 
            else {
            // this will be executed when there are n number of arguments
                  for (var i = 0; i < arguments.length; i++) {
                  // type checking
                          if(typeof arguments[i] == "number" || typeof arguments[i] == "string") {
                                  arguments[i] = Number(arguments[i])
                  }
                  else {
                          throw new TypeError('Unexpected type found') 
                                  }
            
                  }
              return Math.max(...args)
            }
            
          }
      catch(e) {
        return e
      }
   }
}
try {
        document.write(findLargest(1,2,3))
} 
catch(e) {
        document.write(e.message)
}