您好,我正在尝试制作一个仅包含数组中大于/小于某个数字的值的弹出窗口。我该怎么做呢?
<script>
var moe = [3,3.14,4.3,8,9,19,23,24,46,54,87];
var noe = moe.indexOf(23);
function myFunction()
{
alert(noe);
}
function compare(){
for (var i=0;i<moe.length;i++){
if (moe[i]>10){
alert(moe[i]);
}
}
}
</script>发布于 2013-11-12 10:16:58
如下所示:
function compare(){
var out = [];
for (var i=0;i<moe.length;i++){
if (moe[i]>10){
out.push(moe[i]);
}
}
alert(out.join());
}发布于 2013-11-12 10:30:51
让我们来看看这个问题:
给定值的数组:values = [3,3.14,4.3,8,9,19,23,24,46,54,87];.
第一步是计算出过滤,然后如何将结果转换为alert()函数的应用程序的字符串。
(function() {
var i, len, values, value, results, string_value;
values = [3,3.14,4.3,8,9,19,23,24,46,54,87];
results = []; // Empty array which we will build in order during the filter
for (i = 0, len = values.length; i < len; i++) {
value = values[i]; // Not needed; used for readability
if (value > 10) {
results.push(value); // Add this value to the results array
}
}
// Now that we have a result lets convert that to a string
string_value = results.join(", ");
// And output the result with some string concatenation
alert("Filtered results: [ " + string_value + " ]");
// The use of string_value is optional, you could in-line
// this into the alert line
})();https://stackoverflow.com/questions/19919615
复制相似问题