Skip to content

Commit a87e42c

Browse files
committed
Iteration ironhack-labs#4: Calculate the average
1 parent 16f5bee commit a87e42c

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

src/functions-and-arrays.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,76 @@ function sumNumbers(list1){
3737
for(let i=0;i<listSize;i++){
3838
totalSum +=list1[i]
3939
}
40-
return totalSum
40+
if(sumNumbers === ""){
41+
return null
42+
}else{
43+
return totalSum}
44+
4145
}
46+
47+
// function sum(listTobeReduced){
48+
// let totalSumToBeReturned = 0;
49+
// let listSize = listTobeReduced.length
50+
// let itemToBeAdded =0
51+
52+
// for(let i=0;i<listSize;i++){
53+
54+
// // console.log(typeof listTobeReduced[i])
55+
56+
// if(typeof(list1[i]) === 'boolean'){
57+
// if(list1[i] === false){
58+
// itemToBeAdded = 0
59+
// }else{
60+
// itemToBeAdded = 1
61+
// }
62+
// }else if(typeof(list1[i]) === 'string'){
63+
// itemToBeAdded = list1[i].length
64+
// }else if(typeof(list1[i]) === 'number'){
65+
// itemToBeAdded = list1[i]
66+
// }else if(typeof(list1[i]) === 'object'){
67+
// itemToBeAdded = list1[i].length
68+
// }
69+
// totalSumToBeReturned +=itemToBeAdded
70+
// }
71+
// return totalSumToBeReturned
72+
// }
73+
74+
4275
// Iteration #4: Calculate the average
4376
// Level 1: Array of numbers
4477
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
4578

79+
80+
function averageNumbers(ArrayTobeAveraged){
81+
if(ArrayTobeAveraged.length ===0){
82+
return null
83+
}
84+
let lenArray = ArrayTobeAveraged.length
85+
let totalArray = sumNumbers(ArrayTobeAveraged)
86+
return totalArray/lenArray
87+
}
88+
89+
90+
91+
92+
4693
// Level 2: Array of strings
4794
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
4895

96+
function averageWordLength(ArrayOfWords){
97+
if(ArrayOfWords.length===0){
98+
return null
99+
}
100+
let lenArray = ArrayOfWords.length
101+
let totalWordsCount = 0
102+
for(let i=0; i<lenArray; i++){
103+
totalWordsCount += ArrayOfWords[i].length
104+
}
105+
return totalWordsCount/lenArray
106+
}
107+
108+
109+
49110
// Iteration #5: Unique arrays
50111
const wordsUnique = [
51112
'crab',
@@ -61,6 +122,24 @@ const wordsUnique = [
61122
'bring'
62123
];
63124

125+
126+
function uniquifyArray(arrayOfElements){
127+
if(arrayOfElements.length === 0){
128+
return null
129+
}
130+
let uniqueList = []
131+
let lenArray = arrayOfElements.length
132+
133+
for (let i=0; i<lenArray; i++){
134+
console.log(i, arrayOfElements[0],((arrayOfElements[0]) in uniqueList))
135+
136+
if(!((arrayOfElements[0]) in uniqueList)){
137+
uniqueList.push(arrayOfElements[0])
138+
}
139+
console.log(uniqueList)
140+
}
141+
return uniqueList
142+
}
64143
// Iteration #6: Find elements
65144
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
66145

0 commit comments

Comments
 (0)