7
7
* @param {array } arr - An array of integers
8
8
*/
9
9
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?
11
27
}
12
28
13
29
/**
@@ -18,7 +34,18 @@ function leadZeroes(arr) {
18
34
* @param {array } arr - An array of data
19
35
*/
20
36
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?
22
49
}
23
50
24
51
/**
@@ -32,7 +59,25 @@ function setter(arr) {
32
59
* @param num
33
60
*/
34
61
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 ;
35
72
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 ;
36
81
}
37
82
38
83
/**
@@ -44,7 +89,8 @@ function isPrime(num) {
44
89
* @param divisor
45
90
*/
46
91
function modulo ( dividend , divisor ) {
47
-
92
+ var quotient = parseInt ( dividend / divisor ) ;
93
+ return dividend - ( quotient * divisor )
48
94
}
49
95
50
96
/**
@@ -57,7 +103,8 @@ function modulo(dividend, divisor) {
57
103
* @param num
58
104
*/
59
105
function nextFive ( num ) {
60
-
106
+ var remainder = num % 5 ;
107
+ return num + ( 5 - remainder )
61
108
}
62
109
63
110
/**
@@ -69,7 +116,16 @@ function nextFive(num) {
69
116
* @param obj
70
117
*/
71
118
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?
73
129
}
74
130
75
131
module . exports = {
0 commit comments