let myFriends = ["John", "Jane", "Irma", "Anna"]; // -- My friends are ${friend}. function createSentence(friends) { let friendsStr = ""; for (let i = 0; i < friends.length; i++) { if (i === 0) { friendsStr += friends[i]; } else if (i === friends.length-1) { friendsStr += ", and " + friends[i]; } else { friendsStr += ", " + friends[i]; } } let sentence = `My friends are ${friendsStr}!`; return sentence; } console.log(createSentence(myFriends));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter