Help JS! Code is working how it is expected!

Hi everybody.
Please can somebody explein me why my code is not working how it is expected.
I try to create a function that finds a maximum in the array.

const newArr = [2, 200, 455, -98];

const  max = (arr) => {
  if (arr.lenght === 0) {
  let  maxElem = arr[0];
  for (i = 1; i < arr.lenght; i++) {
    if (maxElem < arr[i]) {
      maxElem = arr[i];
  return maxElem;


It returns 2 instead of 455

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Welcome, Amalia.

You have 2 typos in your function.

Click to reveal answer:

You have lenght not length.

Fine! Thank you.
This is a my first post :slightly_smiling_face:

Thank you very much!!!