Skip to content

Instantly share code, notes, and snippets.

@JoshuaRotimi
Created June 24, 2024 13:39
Show Gist options
  • Select an option

  • Save JoshuaRotimi/04a871cd5ffa1861bb5010505cc4b3f7 to your computer and use it in GitHub Desktop.

Select an option

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.
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