Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Exercise Done
  • Loading branch information
Carlos HM authored and Carlos HM committed Oct 23, 2019
commit c0e48acfa2427550b06f3c8fa8c2133e70885722
94 changes: 93 additions & 1 deletion starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
// Find the maximum
const maxOfTwoNumbers = (num1, num2) => {
if(num1 > num2) {
return num1;
} else {
return num2;
}
}

maxOfTwoNumbers(2,1);
maxOfTwoNumbers(1,2);
maxOfTwoNumbers(2,2);


// Finding Longest Word
const words = [
Expand All @@ -11,14 +23,61 @@ const words = [
'crackpot'
];

const findLongestWord = arr =>{
if(!arr.length) {
return null;
} else if (arr.length === 1){
return arr[0];
}


arr.array.forEach(element => {
console.log(element);
if (element.length === arr.length) {
return element;
}

console.log()
});
}

// Calculating a Sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

// Calculate the Average
const sumArray = arr => {
let sum = 0;

if (!arr.length) {
return 0;
}

for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

sumArray(numbers);


// Calculate the Average
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

const averageNumbers = arr => {
let sum = 0;

if (!arr.length) {
return null;
}
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
};

averageNumbers(numbersAvg);

// Array of Strings
const wordsArr = [
'seat',
Expand All @@ -33,6 +92,19 @@ const wordsArr = [
'palace'
];

const averageWordLength = arr => {
let averageWord = 0;

if(!arr.length) {
return null;
}

for(let i = 0; i < arr.length; i++) {
averageWord += arr[i].length;
}
return averageWord / arr.length;
};

// Unique Arrays
const wordsUnique = [
'crab',
Expand All @@ -48,6 +120,26 @@ const wordsUnique = [
'bring'
];

const uniquifyArray = arr => {
if (!arr.length) {
return [];
}

for(let i = 0; i < arr.length; i++) {
//console.log(arr[i]);
for(let j = 0; j < arr.length - 1; j++) {
//console.log(arr[i]);
//console.log('Segunda ' + arr[i+1]);
if(arr[i] === arr[j]) {
arr.splice(arr[j],1);
}
}
}

}

//uniquifyArray(wordsUnique);

// Finding Elements
const wordsFind = [
'machine',
Expand Down