问题描述
我从昨天开始尝试在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));