Created
June 24, 2024 13:39
-
-
Save JoshuaRotimi/04a871cd5ffa1861bb5010505cc4b3f7 to your computer and use it in GitHub Desktop.
Write a function that takes an array of daily temperatures and returns an array where each element is the number of days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const dailyTemp = (arr) => { | |
| const answer = [] | |
| for (let i = 0; i < arr.length; i++) { | |
| const current = arr[i]; | |
| const higher = arr.slice(i).findIndex((item) => item > current); | |
| higher >= 0 ? answer.push(higher) : answer.push(0); | |
| } | |
| return answer; | |
| } | |
| console.log(dailyTemp([70, 70, 70, 75])); | |
| console.log(dailyTemp([90, 80, 70, 60])); | |
| console.log(dailyTemp([73, 74, 75, 71, 69, 72, 76, 73])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment