0

I've tried to figure this out for a couple of hours, but I don't see what I am doing or not doing to make this work. Any help is appreciated. I am getting 2 list of numbers from the user using the following html:

<input type = "text" id = "firstArray"><br/>
<input type = "text" id = "secondArray"><br>

I am then calling the function in my external js file (event handler works and function is called passing the 2 list of numbers):

Answer = largestNumbers(document.getElementById("firstArray").value,
         document.getElementById("secondArray").value);

Then in my js function, I am attaching the 2 list of numbers to an array, making it 2 arrays inside an array. I am wanting to find the largest number of each array and send those 2 numbers to an array (largeList) and send the output to the console.log. Below is the js function:

function largestNumbers(arr, arr1) {
  var otherArray = [];
  var largeList = [];
  otherArray[0] = arr.split(" ");
  otherArray[1] = arr1.split(" ");

  console.log("otherArray:  " + otherArray);
  for (var i = 0; i < otherArray.length; i++) {
      var biggestNum = otherArray[i][0];
      for (var j=0; j < otherArray[i].length; j++) {      
          if (otherArray[i][j] > biggestNum) {
              biggestNum = (otherArray[i][j]);
              console.log("big num:  " + biggestNum);

          } 
      }
    largeList[i] = biggestNum;  
  }
  console.log(largeList);
  return largeList; 
}
  • 2
    What's wrong with using `Math.max` here? Is this homework? – raina77ow Sep 04 '17 at 11:32
  • You can find the answer here https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array – itodd Sep 04 '17 at 11:32
  • 1
    Possible duplicate of [How might I find the largest number contained in a JavaScript array?](https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) – Hassan Imam Sep 04 '17 at 11:33
  • 2
    This is much more complicated than it needs to be, even if you don't use `Math.max`. Divide-and-conquer: Give yourself a function to find the largest number in a single array (`Math.max` will do that if you like: `result = Math.max.apply(Math, theArray)`). Then use that function twice (once on each array), and `return [firstResult, secondResult]`. – T.J. Crowder Sep 04 '17 at 11:33
  • lol...this is not homework. I'm trying to expand my knowledge on arrays. – user2655352 Sep 04 '17 at 11:33
  • 1
    You can also use spread syntax `Math.max(...array)` to find the maximum value inside an array. – Hassan Imam Sep 04 '17 at 11:37
  • @user2655352 did you checked the answer that has been posted? – Ankit Agarwal Sep 04 '17 at 12:01

3 Answers3

1

You can use Math.max.apply for getting the maximum value inside the array.

function getResult(){
var Answer = largestNumbers(document.getElementById("firstArray").value,
         document.getElementById("secondArray").value);
console.log(Answer);
function largestNumbers(arr, arr1) {
  var largeList = [];
  var otherArray = [];
  otherArray[0] = arr.split(" ").map(x=>{return parseInt(x)});
  otherArray[1] = arr1.split(" ").map(x=>{return parseInt(x)});
  debugger
  largeList.push(Math.max.apply(null, otherArray[0]));
  largeList.push(Math.max.apply(null, otherArray[1]));
  
  return largeList; 
}
}
<input type = "text" id = "firstArray"><br/>
<input type = "text" id = "secondArray"><br>
<button id='result' onclick='getResult()'>Get Largest</button>

Notice that the array elements has been converted to integer during split(' '), which will create integer array.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You can use Math.max

function largestNumbers(arr, arr1) {
  return [Math.max.apply(null, arr), Math.max.apply(null, arr1)];
}
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
0

You could use sort beside trim, split, map functions.

Ex:

function getLargeFromInput(input) {
    return input
            .value // get input value
            .trim() // remove spaces from edges
            .split(" ") // convert to array
            .map(Number) // convert array items to be numbers
            .sort((x, y) => y - x) // sort DESC
            [0] // get the largest value
}


function getLargest() {
  
  var val1 = getLargeFromInput(document.querySelector('#inpt1'))
  var val2 = getLargeFromInput(document.querySelector('#inpt2'))       
  var largeList= [val1, val2]
  alert(largeList);    
                  
}
<input id="inpt1" value="1 5 3 -9 44 6"><br>
<input id="inpt2" value="7 23 1 77 33 1">
<br><br>
<input type="button" value="Get largest" onclick="getLargest()">

Another solution without sort function

function largestNumbers(val1, val2) {
  var arr1 = val1.split(" ").map(Number),
      arr2 = val2.split(" ").map(Number),
      largeList = [];
 
  for (var i = 0; i < arr1 .length; i+=1)
      largeList[0] = largeList[0] < allNumbers[i]? allNumbers[i] : largeList[0] 
 
  for (var j = 0; i < arr2 .length; j+=1)
      largeList[1] = largeList[1] < allNumbers[j]? allNumbers[1] : largeList[1] 
 
  return largeList;      
}

function run () {
  var largeList= largestNumbers(
                 document.querySelector('#inpt1').value,
                 document.querySelector('#inpt2').value
           );
  alert(largeList);
}
<input id="inpt1" value="1 5 3 -9 44 6">
<br>
<input id="inpt2" value="7 23 1 77 33 1">
<br><br>
<input type="button" value="Get largest" onclick="run()">
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19