generated from lighthouse-labs/join-concepts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinList.js
35 lines (31 loc) · 1.07 KB
/
joinList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Write a function that joins the contents of conceptList together
* into one String, concepts, with each list item separated from
* the previous by a comma.
*
* To implement this we'll create a joinList function which will take
* in any array of strings return a comma-separated string.
*
* Note: We can NOT use the built-in Array join function.
*/
// Write our function (we must define it too!) below
// ...
const joinList = function(arr) {
// create a string element to return as result
if (arr.length === 0) {
let arrString = '';
return arrString
}
let arrString = arr[0];
for (let i = 1; i < arr.length; i++) {
arrString = `${arrString}, ${arr[i]}`;
}
return arrString;
//loop through the function to convert the elements of array into string
//concatenate the elements together into one string
//add comma between the elements
}
// Test / Driver Code below...
const conceptList = ["gists", "types", "operators", "iteration", "problem solving"];
const concepts = joinList(conceptList);
console.log(`Today I learned about ${concepts}.`);