Skip to content

Commit 5f794a1

Browse files
committed
First round solutions.
1 parent 9063d2b commit 5f794a1

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

roundOne.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@
77
* @param {array} arr - An array of integers
88
*/
99
function leadZeroes(arr) {
10-
10+
// First trial
11+
// Where are all the zeroes? Lets find them and place them here
12+
var indices = [];
13+
arr.forEach(function(item, idx, arr) {
14+
if (item == 0) {
15+
indices.push(idx)
16+
}
17+
});
18+
// Now that we have them, lets remove them.
19+
// For each 0 we remove, add a 0 to the front
20+
for (var i = 0; i < indices.length; i++) {
21+
arr.splice(indices[i], 1);
22+
arr.unshift(0);
23+
}
24+
// Return the same array
25+
return arr
26+
// Can you do this in a different way?
1127
}
1228

1329
/**
@@ -18,7 +34,18 @@ function leadZeroes(arr) {
1834
* @param {array} arr - An array of data
1935
*/
2036
function setter(arr) {
21-
37+
// Gather all the unique items here.
38+
var newArr = [];
39+
// Iterate over the original array to search it
40+
arr.forEach(function(item, idx, arr) {
41+
if (!newArr.includes(item)) {
42+
newArr.push(item)
43+
}
44+
// Only items NOT in newArr need to be added.
45+
});
46+
// Return our unique list!
47+
return newArr;
48+
// Can you do this differently?
2249
}
2350

2451
/**
@@ -32,7 +59,25 @@ function setter(arr) {
3259
* @param num
3360
*/
3461
function isPrime(num) {
62+
if (num === 2 || num === 3) { return true }
63+
// A prime number is the product of itself and 1.
64+
// Suppose 10 x 10 = 100. So is 2 x 50.
65+
// Note that in both cases either the multiples are equal
66+
// or one is greater than the other, to equal 100 together.
67+
// If our number is prime, its multiples will be 1 and itself.
68+
// That's why we only have to check the numbers less than
69+
// the square root of num instead of every number from 1 to num
70+
var sqrt = Math.floor(Math.sqrt(num)) + 1;
71+
var is = true;
3572

73+
// Start with 2 since num % 1 is num
74+
for(var i=2; i < sqrt; i++) {
75+
if(num % i === 0) {
76+
is = false;
77+
break
78+
}
79+
}
80+
return is;
3681
}
3782

3883
/**
@@ -44,7 +89,8 @@ function isPrime(num) {
4489
* @param divisor
4590
*/
4691
function modulo(dividend, divisor) {
47-
92+
var quotient = parseInt(dividend/divisor);
93+
return dividend - (quotient * divisor)
4894
}
4995

5096
/**
@@ -57,7 +103,8 @@ function modulo(dividend, divisor) {
57103
* @param num
58104
*/
59105
function nextFive(num) {
60-
106+
var remainder = num % 5;
107+
return num + (5 - remainder)
61108
}
62109

63110
/**
@@ -69,7 +116,16 @@ function nextFive(num) {
69116
* @param obj
70117
*/
71118
function osort(obj) {
72-
119+
// First lets only worry about the keys. Then sort them.
120+
var keys = Object.keys(obj).sort();
121+
// Create a new object for the sorted data.
122+
var sorted = {};
123+
// Iterate through the keys and set their values
124+
keys.forEach(function(k, idx, arr) {
125+
sorted[k] = obj[k];
126+
});
127+
return sorted
128+
// Can you think of a different way to approach this?
73129
}
74130

75131
module.exports = {

tests/roundOne.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('nextFive()', function() {
5858
});
5959
test('returns next 5 after 0', function() {
6060
expect(r1.nextFive(0)).toEqual(5);
61-
})
61+
});
6262
});
6363

6464
describe('osort()', function() {
@@ -67,8 +67,6 @@ describe('osort()', function() {
6767
});
6868
test('sorts object alphabetically 2', function() {
6969
expect(r1.osort({marketing: true, email: false, phone: false})).toEqual({email: false, marketing: true, phone: false})
70-
})
71-
})
72-
73-
70+
});
71+
});
7472

0 commit comments

Comments
 (0)