Javascript dropdown options 0-100 increment by .5

问题: I have a javascript function that is creating the values for a dropdown on my page. I am currently incrementing by 5, (0-100). I am trying to change this to increment by...

问题:

I have a javascript function that is creating the values for a dropdown on my page. I am currently incrementing by 5, (0-100).

I am trying to change this to increment by .5 instead but it keeps just returning 0-100 with no increment.

My expected output is 0,.5,1,1.5,2,2.5 ... 100.

Here is my function so far:

/**
 * Generate our possible error scores
 */
function generateErrorScores() {

    var min = 0,
        max = 100,
        multiplier = 5, // Tried .5 here
        list = [];

    // Loop between min and max, increment by multiplier
    for (var i = min; i <= max; i++) {
        if (i % multiplier === 0) {
            list.push(i);
        }
    }

    return list;
}

console.log(generateErrorScores())

Fiddle: http://jsfiddle.net/3y451jga/

I have a feeling its if (i % multiplier === 0) { causing the problem but I am not sure how to adapt it to the .5 increments.


回答1:

If all you want is the list [0, 0.5, 1, 1.5, ..., 100] you could do:

/**
 * Generate our possible error scores
 */
function generateErrorScores() {
  const multiplier = 0.5
  return [...Array((100 / multiplier) + 1)].map((x, i) => i * multiplier)
}

console.log(generateErrorScores())


回答2:

The if (i % multiplier === 0) serves no purpose. That works if you need multipliers off a list, but in this case you need fractions.

Interestingly, your comment is correct ("increment by multiplier") but the code is not doing that. A valid solution would be something like this:

function generateErrorScores() {

    var min = 0,
        max = 100,
        segment = .5,
        list = [];

    // Loop between min and max, increment by segment
    for (var i = min; i <= max; i += segment) {
        list.push(i);
    }

    return list;
}

console.log(generateErrorScores())

回答3:

you can just increament the i by 0.5 and you will get your expected output, there is no need for extra check here.

 for (var i = min; i <= max; i+=0.5) {

/**
 * Generate our possible error scores
 */
function generateErrorScores() {
  var min = 0,
    max = 100,
    multiplier = 0.5,
    list = [];
  // Loop between min and max, increment by multiplier
  for (var i = min; i <= max; i += 0.5) {
    list.push(i);
  }
  return list;
}
console.log(generateErrorScores())


回答4:

function generateErrorScores() {

	var min = 0,
		max = 100,
		increment = 0.5,
		list = [];

	// Loop between min and max, increment by multiplier
	for (var i = min; i <= max; i = i + increment) {
		list.push(i);
	}

	return list;

}

console.log(generateErrorScores());


回答5:

I think it would be easier to just do this

for (var i = min; i <= max; i+=0.5) {
    list.push(i);
}
  • 发表于 2019-01-11 23:56
  • 阅读 ( 194 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除