-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin.html
More file actions
72 lines (59 loc) · 2.25 KB
/
join.html
File metadata and controls
72 lines (59 loc) · 2.25 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<script src="../simpleTest.js"></script>
<script>
// NOTES:
// array.string()
// join REQUIREMENTS:
// DONE It should convert the array into a string.
// DONE It should include all elements of the array in the string.
// DONE If second argument is passed in, it should seperate each element from array in the string using the value identified in the second argument.
// DONE If no seperator is included, it should use a comma to seperate elements.
// DONE If seperator is an empty string, it should not seperate elements in string.
// DONE If necessary, it should convert seperator into a string.
function join(array, seperator) {
var joinedArray;
var bucket = [];
if (arguments.length === 2) {
joinedArray = (array[0].toString() + seperator.toString());
for (var i = 1; i < array.length - 1; i++) {
joinedArray = joinedArray + (array[i].toString() + seperator.toString());
}
joinedArray = joinedArray + array[array.length - 1];
return joinedArray;
} else {
joinedArray = array.toString();
return joinedArray;
}
}
tests({
'It should convert the array into a string.': function() {
var myArray = ['element'];
var result = join(myArray);
var whatIsIt = (typeof result);
eq(whatIsIt, 'string');
},
'It should include all elements of the array in the string.': function() {
var myArray = ['first', 'second'];
var result = join(myArray);
eq(result, 'first,second');
},
'If second argument is passed in, it should seperate each element from array in the string using the value identified in the second argument.': function() {
var myArray = ['first', 'second', 'third'];
var result = join(myArray, '-');
},
'If no seperator is included, it should use a comma to seperate elements.': function() {
var myArray = ['first', 'second'];
var result = join(myArray);
eq(result, 'first,second');
},
'If seperator is an empty string, it should not seperate elements in string.': function() {
var myArray = ['first', 'second'];
var result = join(myArray, '');
eq(result, 'firstsecond');
},
'If necessary, it should convert seperator into a string.': function() {
var myArray = ['first', 'second'];
var result = join(myArray, 2);
eq(result, 'first2second')
}
});
</script>