Skip to content

Commit 07880c1

Browse files
authored
solving first question of radio-stations exercise
1 parent 72f97b0 commit 07880c1

1 file changed

Lines changed: 126 additions & 54 deletions

File tree

‎week-2/3-extra/1-radio-stations.js‎

Lines changed: 126 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,74 +25,146 @@
2525
* - Return only the frequencies that are radio stations.
2626
*/
2727
// `getStations` goes here
28+
/*
29+
function getStations() {
30+
var af = getAllFrequencies();
31+
var currentStation = Math.floor(Math.random() * (108 - 87 + 1) + 87);
32+
console.log(currentStation);
33+
isRadioFrequency(currentStation);
34+
return currentStation;
35+
}
36+
function isRadioFrequency(frequency) {
37+
found = false;
38+
var af = getAllFrequencies();
39+
for (var i = 0; i < af.length; i++) {
40+
if (af[i] === frequency) {
41+
found = true;
42+
}
43+
}
44+
45+
return found;
46+
}
47+
*/
2848

49+
//----------------------------------------------------------------------
50+
/**
51+
* Finding a radio station, and a good one, can be hard manually.
52+
* Let's use some code to help us build a program that helps us scan
53+
* the radio waves for some good music.
54+
*/
55+
56+
/**
57+
* First, let's create a function that creates a list of all the frequencies.
58+
* Call this function `getAllFrequencies`.
59+
*
60+
* This function should:
61+
* - Create an array starting at 87 and ending in 108
62+
* - Should return this array to use in other functions
63+
*/
64+
65+
// `getAllFrequencies` goes here
66+
function getAllFrequencies() {
67+
frequenciesArray = [];
68+
for (var i = 0; i <= 108 - 87; i++) {
69+
frequenciesArray[i] = 87 + i;
70+
}
71+
return frequenciesArray;
72+
}
73+
74+
/**
75+
* Next, let's write a function that gives us only the frequencies that are radio stations.
76+
* Call this function `getStations`.
77+
*
78+
* This function should:
79+
* - Get the available frequencies from `getAllFrequencies`
80+
* - There is a helper function called isRadioFrequency that takes an integer as an argument and returns a boolean.
81+
* - Return only the frequencies that are radio stations.
82+
*/
83+
// `getStations` goes here
84+
function getStations() {
85+
var af = getAllFrequencies();
86+
87+
var currentStation = Math.floor(Math.random() * (108 - 87 + 1) + 87);
88+
89+
isRadioFrequency(currentStation);
90+
return currentStation;
91+
}
92+
function isRadioFrequency(frequency) {
93+
found = false;
94+
var af = getAllFrequencies();
95+
for (var i = 0; i < af.length; i++) {
96+
if (af[i] === frequency) {
97+
found = true;
98+
}
99+
}
100+
101+
return found;
102+
}
29103

30104
/* ======= TESTS - DO NOT MODIFY ======= */
31105

32106
function getAvailableStations() {
33-
// Using `stations` as a property as defining it as a global variable wouldn't
34-
// always make it initialized before the function is called
35-
if (!getAvailableStations.stations) {
36-
const stationCount = 4;
37-
getAvailableStations.stations = new Array(stationCount)
38-
.fill(undefined)
39-
.map(function() {
40-
return Math.floor(Math.random() * (108 - 87 + 1) + 87);
41-
})
42-
.sort(function(frequencyA, frequencyB) {
43-
return frequencyA - frequencyB;
44-
});
45-
}
46-
47-
return getAvailableStations.stations;
107+
// Using `stations` as a property as defining it as a global variable wouldn't
108+
// always make it initialized before the function is called
109+
if (!getAvailableStations.stations) {
110+
const stationCount = 4;
111+
getAvailableStations.stations = new Array(stationCount)
112+
.fill(undefined)
113+
.map(function() {
114+
return Math.floor(Math.random() * (108 - 87 + 1) + 87);
115+
})
116+
.sort(function(frequencyA, frequencyB) {
117+
return frequencyA - frequencyB;
118+
});
119+
}
120+
121+
return getAvailableStations.stations;
48122
}
49123

50124
function isRadioStation(frequency) {
51-
return getAvailableStations().includes(frequency);
125+
return getAvailableStations().includes(frequency);
52126
}
53127

54-
const assert = require("assert");
128+
const assert = require('assert');
55129

56130
function test(testName, fn) {
57-
try {
58-
fn();
59-
console.log(`\n✅ ${testName}: PASS`);
60-
} catch (error) {
61-
console.log(
62-
`\n❌ ${testName}: FAIL (see details below)\n\n${error.message}`
63-
);
64-
}
131+
try {
132+
fn();
133+
console.log(`\n✅ ${testName}: PASS`);
134+
} catch (error) {
135+
console.log(`\n❌ ${testName}: FAIL (see details below)\n\n${error.message}`);
136+
}
65137
}
66138

67-
test("getAllFrequencies() returns all frequencies between 87 and 108", function() {
68-
const frequencies = getAllFrequencies();
69-
assert.deepStrictEqual(frequencies, [
70-
87,
71-
88,
72-
89,
73-
90,
74-
91,
75-
92,
76-
93,
77-
94,
78-
95,
79-
96,
80-
97,
81-
98,
82-
99,
83-
100,
84-
101,
85-
102,
86-
103,
87-
104,
88-
105,
89-
106,
90-
107,
91-
108
92-
]);
139+
test('getAllFrequencies() returns all frequencies between 87 and 108', function() {
140+
const frequencies = getAllFrequencies();
141+
assert.deepStrictEqual(frequencies, [
142+
87,
143+
88,
144+
89,
145+
90,
146+
91,
147+
92,
148+
93,
149+
94,
150+
95,
151+
96,
152+
97,
153+
98,
154+
99,
155+
100,
156+
101,
157+
102,
158+
103,
159+
104,
160+
105,
161+
106,
162+
107,
163+
108
164+
]);
93165
});
94166

95-
test("getStations() returns all the available stations", () => {
96-
const stations = getStations();
97-
assert.deepStrictEqual(stations, getAvailableStations());
167+
test('getStations() returns all the available stations', () => {
168+
const stations = getStations();
169+
assert.deepStrictEqual(stations, getAvailableStations());
98170
});

0 commit comments

Comments
 (0)