-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpre-problems.js
More file actions
94 lines (65 loc) · 1.78 KB
/
pre-problems.js
File metadata and controls
94 lines (65 loc) · 1.78 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
/*
Given a file input, output the contents of a file
to the console
*/
const input = require('fs').readFileSync('file.txt', 'utf8');
console.log(input)
/*
Create a constant equal to 1
Write a test to prove it equals 1
Write a test to prove it does not equal anything other than 1
Use "assert", remember to include the library in REPL.it
Note: undefined means the test passed
*/
let assert = require('assert');
const SOME_CONSTANT = 1
assert.equal(SOME_CONSTANT, 1, 'Equal Test failed')
assert.notEqual(SOME_CONSTANT, 2, 'Not Equal Test failed')
/*
1. String to array
Take this string
“1,2,3,4,5”
And use it to create the following array:
[1,2,3,4,5]
*/
const result = "1,2,3,4,5".split(',')
console.log(result)
/*
2. Array to groups of arrays
Given the following file input (string format)
`775 785 361
622 375 125
297 839 375`
Create an object of arrays:
[
[775, 785, 361],
[622, 375, 125],
[297, 839, 375],
]
*/
// use split to split by line (\n character)
const splitted = `775 785 361
622 375 125
297 839 375`.split('\n')
const result = splitted.map(str => str.split(' ').filter(item => item.length > 0).map(item => parseInt(item)))
// use map to create an array
/*
Unique an array
Given the following array, create another array of unique values
[1,1,2,3,4,5,5,5,6,7,7,8,9,10,11,12,13,14,14,15]
*/
const intermediate_result = {}
const input = [1,1,2,3,4,5,5,5,6,7,7,8,9,10,11,12,13,14,14,15]
input.forEach(item => {
intermediate_result[item] = item
})
const result = Object.keys(intermediate_result).map(item => parseInt(item))
console.log(result)
/*
Sum the largest two of three numbers
Given three numbers, find the largest two
and return the sum of those two largest numbers
Example
Given: 1 2 3
Result: 5 (2 + 3)
*/