Skip to content
Exercise · Scope · lesson 8 of 8

Exercise: last positive number

Write a function function lastPositive(nums) that returns the last positive number in the array nums. If the array has no positive numbers, the function returns null.

  • lastPositive([3, -1, 5, -2]) → 5
  • lastPositive([8, -3, -6]) → 8
  • lastPositive([-1, 0, -5]) → null

Constraints: nums is an array of integers of length 0 to 20; 0 does not count as positive; for an empty array and for an array with no positive numbers the function returns null.

Do not add extra imports unless the task says so explicitly.

last.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way