Javascript 替换为<;br>;

Javascript 替换为<;br>;,javascript,arrays,Javascript,Arrays,自从我开始学习编程课程以来,我已经浏览了一段时间了,我发现这是一个很棒的社区,也是学习知识的好地方 目前,我一直在努力清理JavaScript函数 我需要将名称输入到数组中,然后当我运行“start”函数时,它会将名称的数量显示为一个数字,然后在新行上显示每个名称 我已经设法让它工作,但是在每一行的开头都有一个“,”字符。我尝试过各种方法来解决这个问题(使用replace和split+join),但到目前为止运气不佳 var arrName = []; var custName; functi

自从我开始学习编程课程以来,我已经浏览了一段时间了,我发现这是一个很棒的社区,也是学习知识的好地方

目前,我一直在努力清理JavaScript函数

我需要将名称输入到数组中,然后当我运行“start”函数时,它会将名称的数量显示为一个数字,然后在新行上显示每个名称

我已经设法让它工作,但是在每一行的开头都有一个“,”字符。我尝试过各种方法来解决这个问题(使用replace和split+join),但到目前为止运气不佳

var arrName = [];
var custName;

function start(){
    var totalName = 0;
    var count = 0;

    document.getElementById("output").innerHTML = " ";

    while(count < arrName.length){
        totalName++;
        count++;
    };
    document.getElementById("output").innerHTML = "The total names in the array are: " + totalName + "<br />" + arrName;
}

function addName(){
    custName = document.getElementById("custname").value;

    if(!custName){
        alert("Empty Name!");
    }
    else{
        arrName.push(custName + "<br>");
        return custName;}
}
var arrName=[];
客户名称;
函数start(){
var totalName=0;
var计数=0;
document.getElementById(“输出”).innerHTML=“”;
while(计数“+arrName;
}
函数addName(){
custName=document.getElementById(“custName”).value;
如果(!custName){
警报(“空名称!”);
}
否则{
arrName.push(custName+“
”); 返回custName;} }
原因很可能是您正试图显示包含此代码的数组:
document.getElementById(“output”)。innerHTML=“数组中的总名称为:“+totalName+”
“+arrName 以下是我的建议:

var arrName=[];
客户名称;
函数start(){
var totalName=0;
var计数=0;
document.getElementById(“输出”).innerHTML=“”;
while(计数”+arrName.join(“
”); document.getElementById(“输出”).innerHTML=“”; document.getElementById(“输出”).innerHTML=strOutput; } 函数addName(){ custName=document.getElementById(“custName”).value; 如果(!custName){ 警报(“空名称!”); } 否则{ arrName.push(custName); 返回custName;} }
由于不需要WHILE循环,您可以这样跳过它:

var arrName=[];
客户名称;
函数start(){
var totalName=arrName.length;
var strOutput=“数组中的总名称为:”;
strOutput+=totalName+“
”+arrName.join(“
”); document.getElementById(“输出”).innerHTML=“”; document.getElementById(“输出”).innerHTML=strOutput; } 函数addName(){ custName=document.getElementById(“custName”).value; 如果(!custName){ 警报(“空名称!”); } 否则{ arrName.push(custName); 返回客户名称; } }
当控制台直接记录或插入DOM时,数组将表示为
item1、item2、item3、item4

简单地说,
arrName.join(“
”)

此外,您的
while
循环可以简单地替换为

totalName=arrName.length;

count=arrName.length

document.getElementById(“输出”).innerHTML=“数组中的总名称为:“+totalName+”
“+arrName
将数组放入DOM(
arrName
)。当数组字符串化时,
被添加到每个元素之间。将
arrName
更改为
arrName.join(“
”)
,然后您可以从
arrName.push(custName+“
”)中删除

        var arrName = [];
        var custName;

        function start(){
            var totalName = 0;
            var count   = 0;

            document.getElementById("output").innerHTML = " ";

            while(count < arrName.length){
                totalName++;
                count++;
            }
            var strOutput   = "The total names in the array are: ";
            strOutput      += totalName + "<br />" +  arrName.join("<br />");
            document.getElementById("output").innerHTML = " ";
            document.getElementById("output").innerHTML = strOutput;
        }

        function addName(){
            custName = document.getElementById("custname").value;

            if(!custName){
                alert("Empty Name!");
            }
            else{
                arrName.push(custName);
                return custName;}
        }
        var arrName = [];
        var custName;

        function start(){
            var totalName   = arrName.length;
            var strOutput   = "The total names in the array are: ";
            strOutput      += totalName + "<br />" +  arrName.join("<br />");
            document.getElementById("output").innerHTML = " ";
            document.getElementById("output").innerHTML = strOutput;
        }

        function addName(){
            custName = document.getElementById("custname").value;

            if(!custName){
                alert("Empty Name!");
            }
            else{
                arrName.push(custName);
                return custName;
            }
        }