Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

returns string of names, seperated by commas and an ampersand namesObj in javascript

function list(names){
  let answer = [];
  let result;
  
  if (names.length === 0) return '';

  for (let n in names) {
    const values = Object.values(names[n]).join('')

    if (names.length === 1) {
      return values
    } else if (names.length === 2 ) {
      answer.push(values)
      result = answer.join(' & ')
    } else {
      answer.push(values);
      result = answer.join(', ')
      let n = result.lastIndexOf(', ')
      result = result.slice(0, n) + result.slice(n).replace(', ', ' & ');
    }
  }
 
  return result
}
Source by kim-mj.tistory.com #
 
PREVIOUS NEXT
Tagged: #returns #string #seperated #commas #ampersand #namesObj #javascript
ADD COMMENT
Topic
Name
1+4 =