-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestcodesamples.js
More file actions
101 lines (76 loc) · 2.07 KB
/
testcodesamples.js
File metadata and controls
101 lines (76 loc) · 2.07 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// USE THIS JAVASCRIPT FILE TO TEST CODE SAMPLES AND ENSURE THAT THEY RUN.
// CODE SAMPLE 5
function codeSample5(foo) {
let returnVal = ''
for( let i = foo.length - 1; i >= 0; i-- ) {
returnVal += foo[i];
}
return returnVal;
}
// CODE SAMPLE 6
function codeSample6(foo, bar) {
let returnVal = [];
for(let i = 0; i < foo.length; i ++) {
returnVal.push(foo[i]);
}
for(let i = 0; i < bar.length; i ++) {
returnVal.push(bar[i]);
}
return returnVal;
}
// CODE SAMPLE 7
function codeSample7(foo) {
let returnVal = '';
for(let i = 0; i < foo.length; i++) {
returnVal += foo[i];
}
return returnVal;
}
// CODE SAMPLE 8
function codeSample8(foo) {
if (foo <= 0) return [];
if (foo === 1) return [0];
//hint: these are the first 2 numbers in the fibonacci sequence
let returnVal = [0, 1];
//start counting from 3rd index
for (let i = 2; i < foo; i++) {
returnVal.push(returnVal[i - 1] + returnVal[i - 2]);
}
return returnVal;
}
// CODE SAMPLE 9
function codeSample9(foo, bar) {
let returnVal = [];
for(let i = 0; i < bar; i++) {
returnVal.push(foo[i])
}
return returnVal;
}
// CODE SAMPLE 10
function codeSample10(foo) {
let returnVal = [];
for (let i = foo.length - 1; i >= 0; i--) {
//index of smallest element in array foo
let min_index = i;
for(let j = foo.length - 1; j >= 0; j--) {
if(foo[j] < foo[min_index]) {
min_index = j;
}
}
returnVal.push(foo[min_index]);
foo.splice(min_index, 1);
}
return returnVal;
}
// TESTS
// CODE SAMPLE 5
// console.log(codeSample5('hello world'));
// CODE SAMPLE 6
// console.log(codeSample6([1, 2, 3], [4, 5, 6]));
// CODE SAMPLE 7
// console.log(codeSample7(['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']));
// CODE SAMPLE 8
// console.log(codeSample8(10));
// CODE SAMPLE 9
// console.log(codeSample9(['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'], 5));
//console.log(codeSample10([1]))