-2
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;
  } else {
    return "The line is empty"
  }
}

 var lineofpeople = ["katrina", "kevin", "vincent"]

output is 'The line is currently: 1. katrina, 2. kevin, 3. vincent, ' I'm trying to get rid of the last comma after 'vincent'

I've asked this before and got answer way to advanced for me, I'm trying to remove it through an If statement but don't know to to write it.

 if (crrLine > katzDeliLine.length - 1) {
      textToPrint + katzDeliLine.length[crrLine - 1] 
    } else {
      textToPrint + katzDeliLine + ""
    }
vladwoguer
  • 951
  • 1
  • 14
  • 28
Kevin A
  • 63
  • 1
  • 8
  • what about your last [question/answer](https://stackoverflow.com/q/57759676/1447675)? does it work? – Nina Scholz Sep 04 '19 at 17:53
  • 1
    If the version of this question you asked 2 days ago didn't provide the answers you were looking for, consider explaining that to the person who took the time to answer. Perhaps they'll be willing to include an alternative that is more to your preference. – Tyler Roper Sep 04 '19 at 17:55

4 Answers4

1

Use Array.join(). It's more performant than loading the whole string into memory every iteration anyway.

function currentLine(katzDeliLine) {
  if (katzDeliLine.length > 0) {
    var buffer = [];
    var textToPrint = "The line is currently: "
    for (var crrLine = 0; crrLine < katzDeliLine.length; crrLine++) {
      buffer.push((crrLine + 1) + ". " + katzDeliLine[crrLine]);
    }
    return textToPrint+buffer.join(', ');
  } else {
    return "The line is empty"
  }
}

var lineofpeople = ["katrina", "kevin", "vincent"]

console.log(currentLine(lineofpeople));

However, this is a little cleaner:

function currentLine(katzDeliLine) {
  if (katzDeliLine.length) {
    return `the line is currently: ${katzDeliLine.map((i, n)=>`${n+1}. ${i}`).join(", ")}`;
  } else {
    return "The line is empty"
  }
}

var lineofpeople = ["katrina", "kevin", "vincent"]

console.log(currentLine(lineofpeople));
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

You can use map function and join method:

Example:

function currentLine(katzDeliLine) {
    if (katzDeliLine.length > 0) {
        let textToPrint = "The line is currently: ";
        textToPrint += katzDeliLine.map((k, i) => `${i + 1} ${k}`).join(', ');    
        return textToPrint;
     }  
     else {
         return "The line is empty"
     }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

Instead of a for loop, you can map the people's array to add the number before the name and join them with , :

function currentLine(katzDeliLine) {
  if (katzDeliLine.length > 0) {
    var textToPrint = "The line is currently: " + katzDeliLine.map((e, ndx) => (ndx + 1) + '. ' + e).join(',');
    return textToPrint;
  } else {
    return "The line is empty"
  }
}

var lineofpeople = ["katrina", "kevin", "vincent"]

var result = currentLine(lineofpeople);

console.log(result)
Taki
  • 17,320
  • 4
  • 26
  • 47
0

You can use regex to match and replace the final ,

let v = 'name1, name2, name3, name4, name5,'

//use regex to match final , and then replace it with 
v = v.replace(/,$/g,'');
console.log(v)
noone
  • 6,168
  • 2
  • 42
  • 51