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

前端 未结 6 1236
伪装坚强ぢ
伪装坚强ぢ 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:25

    You could map the leading numbers with the value and join the array with comma.

    function currentLine(array) {
        return array.length
            ? `The line is currently: ${array.map((v, i) => `${i + 1}. ${v}`).join(', ')}.`
            : "The line is empty.";
    }
    
    var lineofpeople = ["katrina", "kevin", "vincent"];
    
    console.log(currentLine(lineofpeople));
    console.log(currentLine([]));

提交回复
热议问题