Skip to content

Instantly share code, notes, and snippets.

@WagnerMoreira
Created July 13, 2019 19:12
Show Gist options
  • Save WagnerMoreira/30edca85dd278c9d97544eb01364f0f0 to your computer and use it in GitHub Desktop.
Save WagnerMoreira/30edca85dd278c9d97544eb01364f0f0 to your computer and use it in GitHub Desktop.
const solution = (numbers) => {
// returns zero if no numbers are undefined
if (!numbers.length) {
return 0;
}
const length = numbers.length;
let current, next, largestNumber = 0;
const first = numbers[0];
const last = numbers[length - 1];
// if there is just one number, return it
if (numbers.length === 1) {
return numbers[0];
}
// [125,442,120,182,-403,-47,391,-368,-455,44,-198,-246]'
// look if next number is bigger than the current one, if yes next is assigned to bigger
numbers.map((n, i) => {
// ignores negative numbers;
if (n < 0) {
return;
}
current = n;
previous =numbers[n -1];
console.log('current:', current );
if ( i <= (length - 1) ) {
next = numbers[i + 1];
console.log('next:', next );
}
if (current > next && current > largestNumber) {
console.log('current > next ### The largest number is now:', current);
return largestNumber = current;
} else if (current < next && next > largestNumber) {
console.log('current < next ### The largest number is now:', next);
return largestNumber = next;
}
})
console.log('###meu output', largestNumber);
return largestNumber;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment