-
Code function findMax(arr) { max = -Infinity for (i = 0; i<arr.length; i++){ if (Math.max(arr[i], max) > max) { max = arr[i] } } return max }
Test Cases const chai = require("chai"); const assert = chai.assert; describe("Find max number in array", function() { it("finds the max number in array",function() { assert.strictEqual(findMax([1, 2, 3, 4, 5]), 5) assert.strictEqual(findMax([1, 2, 3, -4, 5]), 5) assert.strictEqual(findMax([-1, -2, -3, -4, -5]), -1) }) })
Output:
-
Code - function findMax(arr) {
arr.sort((a, b) => b - a);return arr[0]- max = -Infinity
- for (i = 0; i<arr.length; i++){
- if (Math.max(arr[i], max) > max) {
- max = arr[i]
- }
- }
- return max
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}