8000 solved problems for fundamental4 · Jamesredux/javascript-exercises@d0405be · GitHub
[go: up one dir, main page]

Skip to content

Commit d0405be

Browse files
committed
solved problems for fundamental4
1 parent f228d73 commit d0405be

File tree

14 files changed

+130
-41
lines changed

14 files changed

+130
-41
lines changed

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

leapYears/leapYears.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
const leapYears = function() {
2-
1+
const leapYears = function(year) {
2+
if(year%4 == 0 && year%400 == 0) {
3+
4+
return true
5+
}
6+
else if(year%4 == 0 && year%100 != 0) {
7+
return true
8+
}
9+
else {
10+
return false
11+
}
312
}
413

514
module.exports = leapYears

leapYears/leapYears.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ describe('leapYears', function() {
44
it('works with non century years', function() {
55
expect(leapYears(1996)).toEqual(true);
66
});
7-
xit('works with non century years', function() {
7+
it('works with non century years', function() {
88
expect(leapYears(1997)).toEqual(false);
99
});
10-
xit('works with ridiculously futuristic non century years', function() {
10+
it('works with ridiculously futuristic non century years', function() {
1111
expect(leapYears(34992)).toEqual(true);
1212
});
13-
xit('works with century years', function() {
13+
it('works with 1 century years', function() {
1414
expect(leapYears(1900)).toEqual(false);
1515
});
16-
xit('works with century years', function() {
16+
it('works with 2 century years', function() {
1717
expect(leapYears(1600)).toEqual(true);
1818
});
19-
xit('works with century years', function() {
19+
it('works with 3 century years', function() {
2020
expect(leapYears(700)).toEqual(false);
2121
});
2222
});

removeFromArray/removeFromArray.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
const removeFromArray = function() {
2-
1+
const removeFromArray = function(array, ...args) {
2+
3+
let newArray = []
4+
5+
for(let i= 0; i< array.length; i++) {
6+
if (!args.includes(array[i])) {
7+
newArray.push(array[i])
8+
}
9+
}
10+
return newArray
311
}
412

513
module.exports = removeFromArray

removeFromArray/removeFromArray.spec.js

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

repeatString/repeatString.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const repeatString = function() {
2-
1+
const repeatString = function(string, multiplier) {
2+
if (multiplier < 0) return "ERROR";
3+
let newString = ""
4+
for (let i = 0; i < multiplier; i++) {
5+
newString += string;
6+
}
7+
return newString
8+
39
}
410

511
module.exports = repeatString

repeatString/repeatString.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ describe('repeatString', function() {
44
it('repeats the string', function() {
55
expect(repeatString('hey', 3)).toEqual('heyheyhey');
66
});
7-
xit('repeats the string many times', function() {
7+
it('repeats the string many times', function() {
88
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
99
});
10-
xit('repeats the string 1 times', function() {
10+
it('repeats the string 1 times', function() {
1111
expect(repeatString('hey', 1)).toEqual('hey');
1212
});
13-
xit('repeats the string 0 times', function() {
13+
it('repeats the string 0 times', function() {
1414
expect(repeatString('hey', 0)).toEqual('');
1515
});
16-
xit('returns ERROR with nega F987 tive numbers', function() {
16+
it('returns ERROR with negative numbers', function() {
1717
expect(repeatString('hey', -1)).toEqual('ERROR');
1818
});
1919
});

reverseString/reverseString.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const reverseString = function() {
2-
1+
const reverseString = function(string) {
2+
let stringLength = string.length
3+
let newString = ""
4+
for(let i = stringLength; i > 0; i--) {
5+
let j = i-1
6+
newString += string.slice(j, i)
7+
}
8+
return newString
39
}
410

511
module.exports = reverseString
12+

reverseString/reverseString.spec.js

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

8-
xit('reverses multiple words', function() {
8+
it('reverses multiple words', function() {
99
expect(reverseString('hello there')).toEqual('ereht olleh')
1010
})
1111

12-
xit('works with numbers and punctuation', function() {
12+
it('works with numbers and punctuation', function() {
1313
expect(reverseString('123! abc!')).toEqual('!cba !321')
1414
})
1515
});

sumAll/sumAll.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
const sumAll = function() {
2-
1+
const sumAll = function(arg1, arg2) {
2+
if (arg1 < 0 || arg2 < 0) return "ERROR";
3+
if (typeof arg1 !== "number" || typeof arg2 !== "number") return "ERROR";
4+
let result = 0
5+
if (arg1 < arg2) {
6+
for(i = arg1; i<= arg2; i++)
7+
result += i
8+
}
9+
else {
10+
for(i = arg2; i<= arg1; i++)
11+
result += i
12+
}
13+
return result
314
}
415

516
module.exports = sumAll

sumAll/sumAll.spec.js

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

tempConversion/tempConversion.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const ftoc = function() {
2-
1+
const ftoc = function(f) {
2+
return Math.round(((f-32)*.5556) * 10)/10
33
}
44

5-
const ctof = function() {
6-
5+
const ctof = function(c) {
6+
return Math.round(((c*1.8)+32) * 10)/10
77
}
88

99
module.exports = {

tempConversion/tempConversion.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ describe('ftoc', function() {
44
it('works', function() {
55
expect(ftoc(32)).toEqual(0);
66
});
7-
xit('rounds to 1 decimal', function() {
7+
it('rounds to 1 decimal', function() {
88
expect(ftoc(100)).toEqual(37.8);
99
});
10-
xit('works with negatives', function() {
10+
it('works with negatives', function() {
1111
expect(ftoc(-100)).toEqual(-73.3);
1212
});
1313
});
1414

1515
describe('ctof', function() {
16-
xit('works', function() {
16+
it('works', function() {
1717
expect(ctof(0)).toEqual(32);
1818
});
19-
xit('rounds to 1 decimal', function() {
19+
it('rounds to 1 decimal', function() {
2020
expect(ctof(73.2)).toEqual(163.8);
2121
});
22-
xit('works with negatives', function() {
22+
it('works with negatives', function() {
2323
expect(ctof(-10)).toEqual(14);
2424
});
2525
});

webpage.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
</head>
6+
<body>
7+
8+
<script>
9+
function add7(number) {
10+
return number + 7
11+
}
12+
13+
function multiply(number1, number2) {
14+
return number1 * number2
15+
}
16+
17+
function capitalize(str) {
18+
str = str.toLowerCase()
19+
let firstLetter = str.slice(0, 1)
20+
return str.replace(firstLetter, firstLetter.toUpperCase())
21+
}
22+
23+
function lastLetter(string) {
24+
return string.slice(string.length-1, string.length)
25+
}
26+
27+
function removeFromArray(array, ...args) {
28+
29+
let newArray = []
30+
for(let i= 0; i< array.length; i++) {
31+
if (!args.includes(array[i])) {
32+
console.log(array)
33+
newArray.push(array[i])
34+
}
35+
}
36+
return newArray
37+
}
38+
39+
40+
console.log(add7(7))
41+
console.log(removeFromArray([1,1,3,4], 1))
42+
console.log(removeFromArray([1,1,1,4], 1))
43+
console.log(lastLetter("abcd frggeh z"))
44+
45+
46+
</script>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)
0