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
154 lines (134 loc) · 4.57 KB
/
Copy pathjavascript.yml
File metadata and controls
154 lines (134 loc) · 4.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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: |-
// this example uses chai
var expect = require("chai").expect;
expect(websites.length, 'The array is still empty').to.not.equal(0);
expect(websites.length, 'The array contains too many values').to.equal(1);
expect(websites[0]).to.equal('codewars');
# async:
# initial: |-
# function solution(client, done){
# }
#
# answer: |-
# const redis = require("redis");
# require('bluebird').promisifyAll(redis.RedisClient.prototype);
#
# const spawn = require('child_process').spawn
#
# function startRedis(ready) {
# return new Promise(resolve => {
# let rs = spawn('redis-server');
# rs.stdout.on('data', (data) => {
# if (data && data.toString().indexOf("Running")){
# resolve(() => process.exit());
# }
# });
# });
# }
#
# function solution(client, done){
# client.setAsync("foo", "bar").then(done);
# }
#
# fixture: |-
# startRedis().then(exit => {
# let client = redis.createClient();
#
# solution(client, _ => {
# describe("redis challenge", true, _ => {
# it ("should have set foo to == bar", done => {
# client.getAsync('foo').then(actual => {
# Test.assertEquals(actual, "bar");
# done();
# });
# });
#
# it ("should have set foo to === bar", done => {
# client.getAsync('foo').then(actual => {
# Test.assertEquals(actual, "bar");
# done();
# });
# });
# }).then(exit);
# });
# });