leetcode 739 code example

Example: Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead javascript

var dailyTemperatures = function(temperatures) {
    let res = Array.from({length:temperatures.length},x=>0);
    let stack = [];
    for(let i=0; i<temperatures.length; i++){
        while(stack.length>0 && temperatures[stack[stack.length-1]]<temperatures[i]){
            let j = stack.pop();
            res[j] = i-j;
        }
        stack.push(i);
    }
    return res;
};