-1

I have this code where it outputs the highest value in document.write using an array that i have declared in that line. This works fine and outputs 56 which is the highest.

  function topProduct(productProfitArray) {
     if (toString.call(productProfitArray) !== "[object Array]")  
       return false;
  return Math.max.apply(null, productProfitArray);
    }

document.write(topProduct([12,34,56,1]));

Now I want to output the value on an declared array also with a string beside the int value, but I get Uncaught SyntaxError: Invalid or unexpected token

var productProfitArray = [ {“Product A”: -75}, {“Product B”: -70}, {“Product C”:
98}, {“Product D”: 5}, {“Product E”: -88}, {”Product F”: 29}];


 function topProduct(productProfitArray) {
     if (toString.call(productProfitArray) !== "[object Array]")  
       return false;
  return Math.max.apply(null, productProfitArray);
    }

document.write(topProduct(productProfitArray));

I tried to add a variable productProfitArray then doing the function and then tried to output the variable using document.write. Any help would be appreciated.

Button Press
  • 623
  • 6
  • 26

1 Answers1

1

You should map the array to get the values:

var productProfitArray = [ {"Product A": -75}, {"Product B": -70}, {"Product C": 98}, {"Product D": 5}, {"Product E": -88}, {"Product F": 29}];

function topProduct(productProfitArray) {
  if (toString.call(productProfitArray) !== "[object Array]")  
    return false;
  return Math.max.apply(null, productProfitArray.map(o => Object.values(o)[0]));
}

document.write(topProduct(productProfitArray));
Mamun
  • 66,969
  • 9
  • 47
  • 59