8000 JS exercise files. · mroy6283/javascript-exercises@033e87e · GitHub
[go: up one dir, main page]

Skip to content

Commit 033e87e

Browse files
committed
JS exercise files.
1 parent 82db5ea commit 033e87e

File tree

10 files changed

+7574
-29
lines changed

10 files changed

+7574
-29
lines changed

01_helloWorld/helloWorld.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const helloWorld = function() {
2-
return ''
2+
return 'Hello, World!'
33
};
44

55
module.exports = helloWorld;

02_repeatString/repeatString.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
const repeatString = function() {
1+
const repeatString = function(string, num) {
2+
let inputString = string
3+
let numReps = num
4+
let outputString = ''
5+
6+
if (numReps < 0) {
7+
return 'ERROR'
8+
} else {
9+
while (numReps > 0) {
10+
outputString += inputString
11+
numReps--
12+
}
13+
return outputString
14+
}
215

316
};
417

02_repeatString/repeatString.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('repeatString', () => {
44
test('repeats the string', () => {
55
expect(repeatString('hey', 3)).toEqual('heyheyhey');
66
});
7-
test.skip('repeats the string many times', () => {
7+
test('repeats the string many times', () => {
88
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
99
});
10-
test.skip('repeats the string 1 times', () => {
10+
test('repeats the string 1 times', () => {
1111
expect(repeatString('hey', 1)).toEqual('hey');
1212
});
13-
test.skip('repeats the string 0 times', () => {
13+
test('repeats the string 0 times', () => {
1414
expect(repeatString('hey', 0)).toEqual('');
1515
});
16-
test.skip('returns ERROR with negative numbers', () => {
16+
test('returns ERROR with negative numbers', () => {
1717
expect(repeatString('hey', -1)).toEqual('ERROR');
1818
});
19-
test.skip('repeats the string a random amount of times', function () {
19+
test('repeats the string a random amount of times', function () {
2020
/*The number is generated by using Math.random to get a value from between
2121
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
2222
equals a number between 0 to 999 (this number will change everytime you run
@@ -31,7 +31,7 @@ describe('repeatString', () => {
3131
was randomly generated. */
3232
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
3333
});
34-
test.skip('works with blank strings', () => {
34+
test('works with blank strings', () => {
3535
expect(repeatString('', 10)).toEqual('');
3636
});
3737
});

03_reverseString/reverseString.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const reverseString = function() {
1+
const reverseString = function(str) {
2+
let newString = ''
23

4+
for (let i = str.length - 1; i >= 0; i--) {
5+
newString += str[i]
6+
}
7+
8+
return newString
39
};
410

511
// Do not edit below this line

03_reverseString/reverseString.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ describe('reverseString', () => {
55
expect(reverseString('hello')).toEqual('olleh');
66
});
77

8-
test.skip('reverses multiple words', () => {
8+
test('reverses multiple words', () => {
99
expect(reverseString('hello there')).toEqual('ereht olleh')
1010
})
1111

12-
test.skip('works with numbers and punctuation', () => {
12+
test('works with numbers and punctuation', () => {
1313
expect(reverseString('123! abc!')).toEqual('!cba !321')
1414
})
15-
test.skip('works with blank strings', () => {
15+
test('works with blank strings', () => {
1616
expect(reverseString('')).toEqual('')
1717
})
1818
});

04_removeFromArray/removeFromArray.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
const removeFromArray = function() {
1+
const removeFromArray = function(array, itemRmv1, itemRmv2, itemRmv3, itemRmv4) {
22

3+
const sourceArray = array
4+
5+
if (itemRmv2 === undefined) {
6+
itemRmv2 = ''
7+
}
8+
if (itemRmv3 === undefined) {
9+
itemRmv3 = ''
10+
}
11+
if (itemRmv4 === undefined) {
12+
itemRmv4 = ''
13+
}
14+
15+
const iterationArray = [itemRmv1, itemRmv2, itemRmv3, itemRmv4]
16+
17+
for (let i of iterationArray) {
18+
19+
let indexNum = sourceArray.indexOf(i)
20+
21+
if (indexNum < 0) {
22+
let notFound = 'Not found!'
23+
}
24+
else {
25+
sourceArray.splice(indexNum,1)
26+
}
27+
}
28+
29+
return sourceArray
330
};
431

32+
533
// Do not edit below this line
634
module.exports = removeFromArray;

04_removeFromArray/removeFromArray.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ describe('removeFromArray', () => {
44
test('removes a single value', () => {
55
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
66
});
7-
test.skip('removes multiple values', () => {
7+
test('removes multiple values', () => {
88
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
99
});
10-
test.skip('ignores non present values', () => {
10+
test('ignores non present values', () => {
1111
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
1212
});
13-
test.skip('ignores non present values, but still works', () => {
13+
test('ignores non present values, but still works', () => {
1414
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
1515
});
16-
test.skip('can remove all values', () => {
16+
test('can remove all values', () => {
1717
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
1818
});
19-
test.skip('works with strings', () => {
19+
test('works with strings', () => {
2020
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
2121
});
22-
test.skip('only removes same type', () => {
22+
test('only removes same type', () => {
2323
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
2424
});
2525
});

05_sumAll/sumAll.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
const sumAll = function() {
1+
const sumAll = function(val1, val2) {
22

3+
let val1Type = typeof val1
4+
let val2Type = typeof val2
5+
let itrVar = 0
6+
7+
if (val1Type !== 'number' || val2Type !== 'number' || val1 < 0 || val2 < 0) {
8+
return 'ERROR'
9+
}
10+
11+
for (let i = Math.min(val1,val2); i < (Math.max(val1,val2) + 1); i++) {
12+
itrVar += i
13+
}
14+
15+
let sum = itrVar
16+
return sum
317
};
418

519
// Do not edit below this line

05_sumAll/sumAll.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('sumAll', () => {
44
test('sums numbers within the range', () => {
55
expect(sumAll(1, 4)).toEqual(10);
66
});
7-
test.skip('works with large numbers', () => {
7+
test('works with large numbers', () => {
88
expect(sumAll(1, 4000)).toEqual(8002000);
99
});
10-
test.skip('works with larger number first', () => {
10+
test('works with larger number first', () => {
1111
expect(sumAll(123, 1)).toEqual(7626);
1212
});
13-
test.skip('returns ERROR with negative numbers', () => {
13+
test('returns ERROR with negative numbers', () => {
1414
expect(sumAll(-10, 4)).toEqual('ERROR');
1515
});
16-
test.skip('returns ERROR with non-number parameters', () => {
16+
test('returns ERROR with non-number parameters', () => {
1717
expect(sumAll(10, "90")).toEqual('ERROR');
1818
});
19-
test.skip('returns ERROR with non-number parameters', () => {
19+
test('returns ERROR with non-number parameters', () => {
2020
expect(sumAll(10, [90, 1])).toEqual('ERROR');
2121
});
2222
});

0 commit comments

Comments
 (0)
0