var ranges = [{"1-10":"Apple"},{"11-20":"Ball"},{"21-30":"Cat"}]
var input_number = 12;
var get_answer = ... // should be "Ball"
HUH函数
浏览 623回答 3
3回答
MMMHUHU
简单的代码给你var ranges = [{"1-10":"Apple"},{"11-20":"Ball"},{"21-30":"Cat"}]function getAns(input_number) { let result = null // handle input ranges for (let index in ranges) { for (let key in ranges[index]) { let min = key.split('-')[0] let max = key.split('-')[1] if (input_number >= min && input_number <= max) { result = ranges[index][key] } } } return result}console.log(getAns(12))