-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.

1
  • 3
    change the quotes in your object keys Commented Mar 6, 2020 at 13:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.