Function 如何连接来自两个不同函数的消息?
大学一年级时,我正在学习编程概念。我的作品有问题Function 如何连接来自两个不同函数的消息?,function,concatenation,Function,Concatenation,大学一年级时,我正在学习编程概念。我的作品有问题 var数组=[13,1,2,3,4,5,6,7,8,9,10,11]; 函数listAll(){ var msg1=“数组中所有值的列表:”+“”; 用于(变量pos=0;pos
var数组=[13,1,2,3,4,5,6,7,8,9,10,11];
函数listAll(){
var msg1=“数组中所有值的列表:”+“
”;
用于(变量pos=0;pos”+”数组中所有值的总和:“+total+”
”;
msg2=msg2+“数组中低于10的所有值的计数:”+count;
msg2=msg1.concat(msg2);
document.getElementById(“msg”).innerHTML=msg2;
}
函数init(){
document.getElementById(“display”).onclick=listAll;
document.getElementById(“stats”).onclick=performStats;
}
window.onload=init代码>这是因为变量的作用域是有限的,您不能在其自身函数之外访问它
(function() {
var array = [13,1,2,3,4,5,6,7,8,9,10,11];
var msg1 = "List of all values in the array: <br>";
var msg2 = "Total number of the values2 in the array: <br>" ;
function listAll(){
// First, no need to do this
// var msg1 = "List of all values in the array: " + "<br>";
// Another way to do a foreach on an array, I find it neater.
// array.forEach(item => {
// msg1 = msg1 + item + " ";
// });
for(var pos = 0; pos < array.length; pos++){
msg1 = msg1 + array[pos]+ " " ;
}
document.getElementById("msg").innerHTML = msg1 ;
}
function performStats(){
var count=0;
var total=0;
for(var index = 0; index < array.length; index++){
total = total + array[index];
if(array[index] < 10)
count++;
}
msg2 = msg2 + array.length;
msg2 = msg2 + "<br/>" + "The sum of all values in the array: "+ total + "<br/>" ;
msg2 = msg2 + "The count of all values below 10 in the array: " + count;
msg2 = msg1.concat(msg2);
document.getElementById("msg").innerHTML= msg2;
}
function init(){
document.getElementById("display").onclick= listAll;
document.getElementById("stats").onclick= performStats;
}
window.onload = init;
})();
但是,您可以在另一个函数中更改它们
(function() {
var array = [13,1,2,3,4,5,6,7,8,9,10,11];
var msg1 = "List of all values in the array: <br>";
var msg2 = "Total number of the values2 in the array: <br>" ;
function listAll(){
// First, no need to do this
// var msg1 = "List of all values in the array: " + "<br>";
// Another way to do a foreach on an array, I find it neater.
// array.forEach(item => {
// msg1 = msg1 + item + " ";
// });
for(var pos = 0; pos < array.length; pos++){
msg1 = msg1 + array[pos]+ " " ;
}
document.getElementById("msg").innerHTML = msg1 ;
}
function performStats(){
var count=0;
var total=0;
for(var index = 0; index < array.length; index++){
total = total + array[index];
if(array[index] < 10)
count++;
}
msg2 = msg2 + array.length;
msg2 = msg2 + "<br/>" + "The sum of all values in the array: "+ total + "<br/>" ;
msg2 = msg2 + "The count of all values below 10 in the array: " + count;
msg2 = msg1.concat(msg2);
document.getElementById("msg").innerHTML= msg2;
}
function init(){
document.getElementById("display").onclick= listAll;
document.getElementById("stats").onclick= performStats;
}
window.onload = init;
})();
一种快速混乱的方法是将变量设置为全局变量或调用另一个函数。甚至是一个自调用函数
(function() {
var array = [13,1,2,3,4,5,6,7,8,9,10,11];
var msg1 = "List of all values in the array: <br>";
var msg2 = "Total number of the values2 in the array: <br>" ;
function listAll(){
// First, no need to do this
// var msg1 = "List of all values in the array: " + "<br>";
// Another way to do a foreach on an array, I find it neater.
// array.forEach(item => {
// msg1 = msg1 + item + " ";
// });
for(var pos = 0; pos < array.length; pos++){
msg1 = msg1 + array[pos]+ " " ;
}
document.getElementById("msg").innerHTML = msg1 ;
}
function performStats(){
var count=0;
var total=0;
for(var index = 0; index < array.length; index++){
total = total + array[index];
if(array[index] < 10)
count++;
}
msg2 = msg2 + array.length;
msg2 = msg2 + "<br/>" + "The sum of all values in the array: "+ total + "<br/>" ;
msg2 = msg2 + "The count of all values below 10 in the array: " + count;
msg2 = msg1.concat(msg2);
document.getElementById("msg").innerHTML= msg2;
}
function init(){
document.getElementById("display").onclick= listAll;
document.getElementById("stats").onclick= performStats;
}
window.onload = init;
})();
(函数(){
var数组=[13,1,2,3,4,5,6,7,8,9,10,11];
var msg1=“数组中所有值的列表:
”;
var msg2=“数组中值2的总数:
”;
函数listAll(){
//首先,没有必要这样做
//var msg1=“数组中所有值的列表:”+“
”;
//另一种在数组上进行foreach的方法,我发现它更整洁。
//array.forEach(项=>{
//msg1=msg1+item+“”;
// });
用于(变量pos=0;pos”+”数组中所有值的总和:“+total+”
”;
msg2=msg2+“数组中低于10的所有值的计数:”+count;
msg2=msg1.concat(msg2);
document.getElementById(“msg”).innerHTML=msg2;
}
函数init(){
document.getElementById(“display”).onclick=listAll;
document.getElementById(“stats”).onclick=performStats;
}
window.onload=init;
})();