我需要一个函数,该函数输出一个值,该值的范围取决于将迭代与极限进行比较的位置。
var limit = 90; //Can change
var range = 3; //1,2,3 - Can change
var iteration = 0;
function {
var output = //number between 0 and range-1
//dependent on iteration/limit;
console.log(output);
iteration++;
}
如果迭代次数为18,则输出为0
如果迭代为35,则输出为1
如果迭代为86,则输出为2
答案 0 :(得分:1)
var limit = 90; //Can change
var range = 3; //1,2,3 - Can change
var iteration = 0;
function {
var output = parseInt(iteration/(limit/range));
console.log(output);
iteration++;
}
t.m。在评论中得到答复
答案 1 :(得分:1)
您可以获取分数并将其乘以所需范围。
const getPart = (limit, range) => value => Math.floor(range * value/ limit);
var getRange = getPart(90, 3);
console.log(getRange(18)); // 0
console.log(getRange(35)); // 1
console.log(getRange(86)); // 2