Home >Web Front-end >JS Tutorial >JavaScript Fun Question: Fragrance Vaporizer

JavaScript Fun Question: Fragrance Vaporizer

黄舟
黄舟Original
2017-02-04 15:47:461210browse

This topic is to test an evaporator filled with fragrance to see its service life.

We know the capacity content of the evaporator (calculated in ml), and the fragrance contained in it will evaporate a certain percentage every day (evap_per_day).

This evaporator requires at least threshold (percentage) of fragrance, otherwise it can no longer be used.

All numbers are positive.

How many days will it take for the evaporator to fail?

The function prototype is as follows:

function evaporator(content, evap_per_day, threshold)

The parameters are capacity, volatilization percentage, and minimum percentage.

For this question, it actually doesn’t matter whether the capacity is used or not. It can also be solved using only percentages, but I think capacity is better to understand.

The following is the solution to the capacity. It determines how much capacity remains after evaporation every day until the current capacity is less than the minimum limit capacity and returns the number of days.

function evaporator(content, evap_per_day, threshold){   
    var day = 0;  
    threshold = content * threshold / 100;  
    while(content >= threshold){  
        content *= (1 - evap_per_day / 100);  
        day++;  
    }  
    return day;  
}

The above is the content of JavaScript fun question: Fragrance evaporator. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn