不同范围的百分比值

问题描述

我从昨天开始尝试在JavaScript中找到一个公式(也是数学公式),该公式返回3种不同情况下给定百分比的值。

var element = createElement('p');
//If addEventListener is needed
element.addEventListener('click',function(){},false);

解决方法

这是您的公式: 试试这个。

const percentage = function(x,y,perc){
    // x is start point
    // y is end point
    // so you need length of this range (between x and y) and we subtract x from y
    // and dividing to 100 (because 100% is full range between x and y)
    // when we divide it to 100,the result is 1% of the range
    // then we multiply it to percentage we want for example 25%
    // and adding x to result. Because we started from x;



    return ((y-x)/100)*perc+x;
}

console.log(percentage(0,10,25));
console.log(percentage(0,100,50));
console.log(percentage(-5,5,50));
console.log(percentage(-10,25));
console.log(percentage(-11,-1,30));

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...