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) {
return;
}
let maxElem = arr[0];
for (i = 1; i < arr.lenght; i++) {
if (maxElem < arr[i]) {
maxElem = arr[i];
}
}
return maxElem;
};
console.log(max(newArr));
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.