This repository was archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathjavascript.yml
More file actions
102 lines (84 loc) · 3.19 KB
/
Copy pathjavascript.yml
File metadata and controls
102 lines (84 loc) · 3.19 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
102
cw-2:
algorithms:
initial: |-
// return the two oldest/oldest ages within the array of ages passed in.
// it should return the two ages as a sorted array, youngest age first
function twoOldestAges(ages){
}
answer: |-
function twoOldestAges(ages){
var oldest = 0, nextOldest;
for(var i = 0;i < ages.length;i++){
var age = ages[i];
if (age > oldest){
nextOldest = oldest;
oldest = age;
}
else if(age > nextOldest){
nextOldest = age;
}
}
return [nextOldest, oldest];
}
fixture: |-
Test.describe("twoOldestAges([1,5,87,45,8,8])", function() {
var results1 = twoOldestAges([1, 5, 87, 45, 8, 8]);
Test.it("Should return something that isn't falsy", function() {
Test.expect(results1, "Something is wrong, twoOldestAges([1,5,87,45,8,8]) has no results!");
});
Test.it("Should return [45,87]", function() {
Test.assertEquals(results1[0], 45, "twoOldestAges([1,5,87,45,8,8]) should return 45 as the second highest result");
Test.assertEquals(results1[1], 87, "twoOldestAges([1,5,87,45,8,8]) should return 87 as the second highest result");
});
});
Test.describe("twoOldestAges([6,5,83,5,3,18])", function() {
var results2 = twoOldestAges([6, 5, 83, 5, 3, 18]);
Test.assertSimilar(results2, [18, 83]);
});
bug fixes:
initial: |-
function Person(name){
this.name = name
}
//TODO: The greet function is not returning the expected value.
Person.prototype.greet = function(){
return "Hello my name is " + name
}
answer: |-
function Person(name){
this.name = name
}
Person.prototype.greet = function(){
return "Hello my name is " + this.name;
}
fixture: |-
var jack = new Person("Jack");
var jill = new Person("Jill");
Test.expect(jack.name == "Jack", "person.name does not have a valid value");
Test.expect(jack.greet, "greet method does not exist on the Person instance");
Test.expect(jack.greet() === "Hello my name is Jack");
Test.expect(jill.greet() === "Hello my name is Jill");
refactoring:
initial: |-
// TODO: This method needs to be called multiple times for the same person (myName).
// It would be nice if we didnt have to always pass in myName every time we needed to great someone.
function greet(myName, yourName){
return "Hello " + yourName + ", my name is " + myName;
}
answer: |-
function Person(name){
this.name = name
}
Person.prototype.greet = function(yourName){
return "Hello " + yourName + ", my name is " + this.name
}
reference:
initial: |-
var websites = [];
answer: |-
// add the values "codewars" to the websites array
var websites = ['codewars'];
fixture: |-
Test.expect(websites.length > 0, 'The array is still empty')
Test.expect(websites.length == 1, 'The array contains too many values')
Test.expect(websites[0] == 'codewars', 'The array does not contain the correct value "codewars"')