@ShahanKrakirian/Fundamentals of JavaScript II Exercises Array Iterations
JavaScript

No description

fork
loading
repl talk
(games)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//-----------------------------------------------------------------
//1. Write a function `sum` that computes the sum of the numbers in an array.

//Solution 

// function sum(array) {
//   var total = 0;
//   for (i = 0; array.length > i; i++) {
//     total = array[i] + total;
//   }
//   return total;
// }

// sum([1, 3, 6]);

//--------------------------------------------------------------
// 2. Write a function `max` that accepts an array of numbers and returns the
//   *largest* number in the array.
   
// function max(array) {
//   var maxValue = 0
//   for (i = 0; array.length >= i; i++) {
//     if (array[i] >= maxValue) {
//       maxValue = array[i];
//     }
//   }
//   return maxValue
// }

// max([1, 5, 10, 15]);
   
//--------------------------------------------------------------
// 3. Try the following at a console:

//   ```js
//   "the quick brown fox jumped over the lazy dog".split(" ");
//   "Hello, world!".split("")
//   "1,2,3,4,5,6".split(",")
//   ```

//   What is returned by `split` (You can read more about it
//   [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)),
//   and how does it work?

//   Use `split` to write a function `longestWord` that takes a string as an
//   argument and returns the longest word.
   
//Solution ***FIX THIS*** keep track of WORDS not of character lengths.
   
// function longestWord(string) {
//   var maxValue = 0;
//   var longest = ''
//   var words = string.split(' ');
//   for (i = 0; words.length > i; i++) {
//     if (words[i].length > maxValue) {
//       longest = words[i];
//       maxValue = longest.length;
//     }
//   }
//   return longest
// }

// longestWord('This is my string')

//--------------------------------------------------------------
// 4. Write a function `remove` that accepts an *array* and an *element*, and
//   returns an array with all ocurrences of *element* removed.

//   ```js
//   function remove(array, element) {
//     // your code here
//   }
//   remove([1, 3, 6, 2, 3], 3); // => [1, 6, 2]
//   ```   
   
//Solution 

// function remove(array, element) {
//   var finalArray = []
//   for (i=0; i < array.length; i++) {
//     if (array[i] !== element) {
//       finalArray.push(array[i]); 
//     }
//   } 
//   return finalArray;
// }
   
// remove([1, 2, 3, 3, 4, 5], 3);
   
//--------------------------------------------------------------

// 5. Write a function `evens` that accepts an array as an argument, and returns
//   an array consisting of all of the *even* numbers in that array.
   
//Solution 

function evens(array) {
  finalArray = [];
  for (i=0; i < array.length; i++) {
    if (array[i] % 2 === 0) {
      finalArray.push(array[i]);
    }
  }
  return finalArray;
}
   
// evens([1, 2, 3, 4, 5, 6, 7, 8]);

//--------------------------------------------------------------
   
// 5. Write a function `evenLengthWords` that takes an array of *strings* as an
//   argument, and returns an array of just the words that have an even length.
   
//Solution 

function evenLengthWords(array) {
  var finalArray = [];
  for (i=0; i < array.length; i++) {
    if (array[i].length % 2 === 0) {
      finalArray.push(array[i]);
    }
  }
  return finalArray
}
   
// evenLengthWords(['keep', 'don\'t']);
   
//--------------------------------------------------------------
//ADVANCED
//--------------------------------------------------------------

// 1. Read about the `join` method on
//   [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
//   and use it to implement a function that accepts a string as an argument and
//   returns that string *reversed*.
   
//Solution 

// function join(string) {
//   var finalString = '';
//   var howManyCharacters = string.length;
//   for (i=0; i < string.length; i++) {
//     finalString = finalString + string[howManyCharacters - 1];
//     howManyCharacters = howManyCharacters - 1; 
//   }
//   return finalString;
// }

// join("sdrawkcab backwards");
  
//Solution using "join"

// function join(string) {
//   var stringArray = string.split('');
//   var backwardsArray = [];
//   var backwardsCount = string.length;
//   for (i=0; i < string.length; i++) {
//     backwardsArray.push(stringArray[backwardsCount - 1]);
//     backwardsCount = backwardsCount - 1; 
//   }
//   var backwardsString = backwardsArray.join('');
//   return backwardsString;
// }

// join("hello people");


//--------------------------------------------------------------

// 2. Write a function `keep` that "keeps" certain elements in an array. The
//   function will need to take *two* arguments, an array, and something else --
//   the second argument will be what is used to determine which elements to keep.

// You should be able to use this function to write `evens`, `evenLengthWords`,
//   a hypothetical `odds` function, or `oddLengthWords` *without changing the
//   `keep` function*.

//Solution 

// function keep(array, condition) {
//   return condition(array);
// }

// keep(['one', 'two', 'four'], evenLengthWords);







Native Browser JavaScript