Skip to content

Commit 209cc7a

Browse files
committed
Lab fixed
1 parent 185cac1 commit 209cc7a

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/functions-and-arrays.js

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ function maxOfTwoNumbers(num1, num2) {
77
}
88
}
99
maxOfTwoNumbers(3,2);
10-
1110
//Iteration #2: Find longest word
1211
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
13-
1412
function findLongestWord(array) {
1513
let longestWord = null;
16-
for (let i=0; i<array.length; i++){
17-
if(array[i].length > longestWord.length){
18-
longestWord = array[i];
14+
if (array.length>0){
15+
longestWord=" ";
16+
for (let i=0; i<array.length; i++){
17+
if(array[i].length > longestWord.length){
18+
longestWord = array[i];
19+
}
1920
}
2021
}
2122
return longestWord;
2223
}
2324
findLongestWord(words);
24-
25-
// Iteration #3: Calculate the sum
25+
/// Iteration #3: Calculate the sum
2626
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
27-
2827
function sumNumbers(arrayOfNumbers) {
2928
let result = 0;
3029
for(i = 0; i < arrayOfNumbers.length; i++){
@@ -33,35 +32,38 @@ function sumNumbers(arrayOfNumbers) {
3332
return result;
3433
}
3534
sumNumbers(numbers);
36-
3735
// Iteration #3.1 Bonus:
3836
function sum() {}
39-
4037
// Iteration #4: Calculate the average
4138
// Level 1: Array of numbers
39+
// What do we do with the sum? what does the number of elements contain
4240
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
43-
4441
function averageNumbers(elements) {
45-
const calc = sumNumbers(elements) / elements.length;
46-
return calc;
42+
let calc=null;
43+
if(elements.length>0){
44+
calc = sumNumbers(elements) / elements.length;
45+
}
46+
return calc;
4747
}
4848
averageNumbers(numbersAvg);
49-
5049
// Level 2: Array of strings
50+
// What does the arrayofwords contain? Should I return the length of all words in the array?
5151
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
5252
function averageWordLength(words) {
53-
let totalCharacters = 0;
54-
for (let i=0; i<words.length; i++){
55-
totalCharacters += words[i].length;
56-
}
57-
return totalCharacters / words.length;
53+
let totalChar=null;
54+
if(words.length>0){
55+
for (let i=0; i<words.length; i++){
56+
totalChar += words[i].length;
57+
}
58+
totalChar=totalChar / words.length;
59+
}
60+
return totalChar;
5861
}
59-
averageWordLength(wordsArr);
60-
62+
averageWordLength(wordsArr)
6163
// Bonus - Iteration #4.1
6264
// return array without duplicates
65+
//
6366
function avg() {}
64-
6567
// Iteration #5: Unique arrays
6668
const wordsUnique = [
6769
'crab',
@@ -77,28 +79,35 @@ const wordsUnique = [
7779
'bring'
7880
];
7981
function uniquifyArray(array) {
80-
let uniqueArr = [];
81-
for (let i=0; i<array.length; i++){
82-
if (uniqueArr.indexOf(array[i]) === -1){
83-
uniqueArr.push(array[i])
82+
let uniqueArr=[];
83+
if(array.length===0){
84+
uniqueArr=null;
85+
}
86+
if(array.length>0){
87+
for (let i=0; i<array.length; i++){
88+
if (uniqueArr.indexOf(array[i]) === -1){
89+
uniqueArr.push(array[i]);
90+
}
8491
}
8592
}
8693
return uniqueArr;
8794
}
8895
uniquifyArray(wordsUnique);
89-
9096
// Iteration #6: Find elements
9197
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
9298
function doesWordExist(wordsArr, word) {
93-
if (wordsArr.includes(word)){
94-
return true;
95-
} else {
96-
return false;
99+
let wordIsOk=null;
100+
if(wordsArr.length>0){
101+
if (wordsArr.includes(word)){
102+
wordIsOk=true;
103+
} else {
104+
wordIsOk=false;
105+
}
97106
}
107+
return wordIsOk;
98108
}
99-
doesWordExist(wordsFind, "monkey");
100-
doesWordExist(wordsFind, "subset");
101-
109+
doesWordExist(wordsFind, "monkey" )
110+
doesWordExist(wordsFind, "subset")
102111
// Iteration #7: Count repetition
103112
const nullTestArray = []
104113
const wordsCount = [
@@ -121,11 +130,10 @@ function howManyTimes(repeatingWords, thisWord ) {
121130
wordCounter++
122131
}
123132
}
124-
return wordCounter;
133+
return wordCounter
125134
}
126-
howManyTimes(wordsCount, "trouble");
127-
howManyTimes(nullTestArray, []);
128-
135+
howManyTimes(wordsCount, "trouble")
136+
howManyTimes(nullTestArray, [])
129137
// Iteration #8: Bonus
130138
const matrix = [
131139
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
@@ -150,7 +158,6 @@ const matrix = [
150158
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
151159
];
152160
function greatestProduct() {}
153-
154161
// The following is required to make unit tests work.
155162
/* Environment setup. Do not modify the below code. */
156163
if (typeof module !== 'undefined') {
@@ -167,4 +174,4 @@ if (typeof module !== 'undefined') {
167174
howManyTimes,
168175
greatestProduct
169176
};
170-
};
177+
};

0 commit comments

Comments
 (0)