How can I remove the last comma in my for loop?

前端 未结 6 1142
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 18:54
function currentLine(katzDeliLine) {
    if (katzDeliLine.length > 0) {
        var textToPrint = \"The line is currently: \"

        for (var crrLine = 0; crrLine &         


        
6条回答
  •  借酒劲吻你
    2021-01-26 19:07

    function currentLine(katzDeliLine) {
        if (katzDeliLine.length > 0) {
            var textToPrint = "The line is currently: "  
    
            for (var crrLine = 0;  crrLine < katzDeliLine.length; crrLine++) {
    
            textToPrint = textToPrint + (crrLine + 1)+". " 
            +katzDeliLine[crrLine]+","
    
    }
    
    
    return textToPrint.replace(/,\s*$/, "");
    } else {
    return "The line is empty"
    }
    }
    
    var lineofpeople = ["katrina", "kevin", "vincent"]
    console.log(currentLine(lineofpeople));
    

提交回复
热议问题