Skip to content

Commit 8eabe22

Browse files
author
rs41
committed
init access array values challenge
1 parent 45c6380 commit 8eabe22

7 files changed

Lines changed: 111 additions & 2 deletions

File tree

menu.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"ARRAYS",
1313
"ARRAY FILTERING",
1414
"LOOPING THROUGH ARRAYS",
15+
"ACCESSING ARRAY VALUES",
1516
"OBJECTS",
1617
"OBJECT PROPERTIES",
1718
"FUNCTIONS",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var path = require('path');
2+
var getFile = require('../../get-file');
3+
var compare = require('../../compare-solution');
4+
5+
var problemName = __dirname.split(path.sep);
6+
problemName = problemName[problemName.length-1];
7+
8+
exports.problem = getFile(path.join(__dirname, 'problem.md'));
9+
exports.solution = getFile(path.join(__dirname, 'solution.md'));
10+
11+
var solutionPath = path.resolve(__dirname, "../../solutions", problemName, "index.js");
12+
var troubleshootingPath = path.resolve(__dirname, "../../troubleshooting.md");
13+
14+
exports.verify = function (args, cb) {
15+
16+
var attemptPath = path.resolve(process.cwd(), args[0]);
17+
compare(solutionPath, attemptPath, function(match, obj) {
18+
19+
if(match) {
20+
return cb(true);
21+
}
22+
23+
if(!obj) {
24+
// An error occured, we've already printed an error
25+
return;
26+
}
27+
28+
var message = getFile(troubleshootingPath);
29+
30+
message = message.replace(/%solution%/g, obj.solution);
31+
message = message.replace(/%attempt%/g, obj.attempt);
32+
message = message.replace(/%diff%/g, obj.diff);
33+
message = message.replace(/%filename%/g, args[0]);
34+
35+
exports.fail = message;
36+
37+
cb(false);
38+
39+
});
40+
};
41+
42+
exports.run = function (args) {
43+
require(path.resolve(process.cwd(), args[0]));
44+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
3+
# ACCESSING ARRAY VALUES
4+
5+
Array elements can be accessed through index number.
6+
7+
Index number starts from zero to array's property length minus one.
8+
9+
Here is an example:
10+
11+
12+
```js
13+
var pets = ['cat', 'dog', 'rat'];
14+
15+
console.log(pets[0]);
16+
```
17+
18+
The above code will print the first element of `pets` array - string `cat`.
19+
20+
Array elements must be accessed through only using bracket notation.
21+
22+
Dot notation is invalid.
23+
24+
Valid notation:
25+
26+
```js
27+
console.log(pets[0]);
28+
```
29+
30+
Invalid notation:
31+
```
32+
console.log(pets.1);
33+
```
34+
35+
## The challenge:
36+
37+
Create a file named `accessing-array-values.js`.
38+
39+
In that file, define array `food` :
40+
```js
41+
var food = ['apple', 'pizza', 'pear'];
42+
```
43+
44+
45+
Use `console.log()` to print the `second value of array to the terminal.
46+
47+
Check to see if your program is correct by running this command:
48+
49+
`javascripting verify accessing-array-values.js`
50+
51+
---
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
3+
# SECOND ELEMENT OF ARRAY PRINTED!
4+
5+
Good job accessing that element of array.
6+
7+
In the next challenge we will work on an example of looping through arrays.
8+
9+
Run `javascripting` in the console to choose the next challenge.
10+
11+
---

problems/array-filtering/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Good job filtering that array.
66

7-
In the next challenge we will work on an example of looping through arrays.
7+
In the next challenge we will work on an example of accessing array values.
88

99
Run `javascripting` in the console to choose the next challenge.
1010

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ Include the name `javascripting` and the name of the challenge you're working on
6161

6262
Add these challenges:
6363

64-
- "ACCESSING ARRAY VALUES"
6564
- "OBJECT KEYS"
6665
- "FUNCTION RETURN VALUES"
6766
- "THIS"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var food = ['apple', 'pizza', 'pear'];
2+
3+
console.log(food[1]);

0 commit comments

Comments
 (0)