Skip to content

Commit f8f03c0

Browse files
committed
still to do level2, Iteration ironhack-labs#4.1, Iteration ironhack-labs#7 Iteration ironhack-labs#8
1 parent 0ea4c7e commit f8f03c0

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

src/functions-and-arrays.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,33 @@ console.log("the max of two numbers is " + maxOfTwoNumbers(2,4));
1717
// Iteration #2: Find longest word
1818
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1919

20-
findLongestWord = (words) => words.reduce((a, b) =>
21-
{if (b.length > a.length) return b; else return a});
20+
21+
function findLongestWord (words) {
22+
23+
let longestWord="";
24+
25+
for(var i=0; i < words.length; i++){
26+
27+
if (longestWord.length < words[i].length){
28+
29+
longestWord = words[i];
30+
31+
}else if(longestWord.length === words[i].length){
32+
33+
var morelongestWords = [];
34+
morelongestWords.push(words[i] , longestWord);
35+
36+
}
37+
}
38+
return longestWord;
39+
}
40+
2241

2342
console.log("the longest word is " + findLongestWord(words));
2443

2544

2645

46+
2747
// Iteration #3: Calculate the sum
2848
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
2949

@@ -113,14 +133,38 @@ const wordsUnique = [
113133
'bring'
114134
];
115135

116-
function uniquifyArray() {}
136+
function uniquifyArray(arr) {
137+
let newArr = []
138+
if(!arr.length){
139+
return null
140+
}
141+
if(arr.length){
142+
for(let i = 0; i < arr.length; i++){
143+
if(!newArr.includes(wordsUnique[i])){
144+
newArr.push(arr[i]);
145+
}}
146+
}
147+
return newArr;
148+
}
149+
console.log("unique array is " + uniquifyArray(wordsUnique))
117150

118151

119152

120153
// Iteration #6: Find elements
121154
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
122155

123-
function doesWordExist() {}
156+
function doesWordExist(wordsFind, wordToSearch){
157+
if(wordsFind.length === 0){
158+
return false;
159+
}
160+
var longitud = wordsFind.length;
161+
for(var i=0; i<longitud; i++){
162+
if(wordsFind[i] === wordToSearch){
163+
return true;
164+
}
165+
}
166+
return false;
167+
}
124168

125169

126170

@@ -139,7 +183,19 @@ const wordsCount = [
139183
'matter'
140184
];
141185

142-
function howManyTimes() {}
186+
187+
function howManyTimes(arr, wordtoSearch) {
188+
if (!arr.length){
189+
return 0;
190+
}
191+
192+
let count = 0
193+
for (let key of arr){
194+
if (key === wordtoSearch)
195+
count++;
196+
}
197+
return count;
198+
}
143199

144200

145201

0 commit comments

Comments
 (0)