Question

In: Computer Science

I need some asistance understanding of what to do for this problem in javascript: * Problem...

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);

  });

});

Solutions

Expert Solution

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)
}

Related Solutions

I need a couple sentences about Creating and testing JavaScript.
I need a couple sentences about Creating and testing JavaScript.
I need to do a research paper on self-esteem and job performance. I just need some...
I need to do a research paper on self-esteem and job performance. I just need some ideas to get me started
I'm not understanding quite how to do this type of problem. The problem states: What is...
I'm not understanding quite how to do this type of problem. The problem states: What is the enthalpy change produced by the reaction of 50.0 g Fe2O3 with 50.0 g Al according to the equation below? Fe2O3(s) + 2Al(s) => Al2O3(s) + 2Fe(s) Delta H = -851.5 kJ Please show me how to do this!
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
I need to design and implement a JAVASCRIPT program that will allow us to determine the...
I need to design and implement a JAVASCRIPT program that will allow us to determine the length of time needed to pay off a credit card balance, as well as the total interest paid. The program must implement the following functions: displayWelcome This function should display the welcome message to the user explaining what the program does. calculateMinimumPayment This function calculates the minimum payment. It should take balance and minimum payment rate as arguments and return the minimum payment. So...
Where would I need to go and what would I need to do in order to...
Where would I need to go and what would I need to do in order to obtain the trademark? (please list all of the steps for this process from beginning to end)
How should marketers go about understanding their customers? What are some of the things they need...
How should marketers go about understanding their customers? What are some of the things they need to do? To create a need, the message content has to show/share the value of the product/service being advertised?
what do we need to consider in understanding the effects of psychoactive drugs in a human
what do we need to consider in understanding the effects of psychoactive drugs in a human
Hello, I need some advice on the case below. I need to know some arguments for...
Hello, I need some advice on the case below. I need to know some arguments for how Kant's ethics applies to this case, and why other people who believe in the Kantean theory of ethics would also support the arguments. I need to use Kants theory to determine if this is morally correct or incorrect. Casino Gambling on Wall Street Case 4.5 Casino Gambling on Wall Street CDO stands for “ collateralized debt obligation,” and before the financial meltdown of...
Statement: I do not need to know accounting. I can hire an accountant. Problem statement: If...
Statement: I do not need to know accounting. I can hire an accountant. Problem statement: If you do not know accounting, how do you know your business is performed effectively? Open ended questions: The impact of disruptive technologies on accounting education, how should you as a business and technology student adapt? (100-500 words) accounting practice, how should you as a business and technology student get ready for working? (100-500 words) business practice, how should business can get ready for the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT