Skip to content

[FT-RMT-102021] JoseL_Valdes #2643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
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
175 changes: 117 additions & 58 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,148 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}



function maxOfTwoNumbers(number1, number2) {
if (number1 > number2) {
return number1;
} else if (number2 > number1) {
return number2;
} else {
return number1, number2;
}
}
// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}


function findLongestWord(someArray) {
if (someArray.length == 0) {
return null;
}
let longestWord = '';
for (i = 0; i < someArray.length; i++) {
if (typeof someArray === 'undefined') {
longestWord = null;
} else if (someArray[i].length > longestWord.length) {
longestWord = someArray[i];
}
}
return longestWord;
}

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

function sumNumbers() {}


function sumNumbers(numbersArray) {
if (numbersArray.length == 0) {
return 0;
}
let sumResult = 0;
for (let i = 0; i < numbersArray.length; i++) {
if (typeof numbersArray[i] === 'object') {
throw new Error("Unsupported data type sir or ma'am");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creo que aquí ya estoy haciendo lo que me comentaste, pero en el test devuelve error. Por favor, ¿podrías indicarme que está mal?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si te fijas, el test que fallaba era de la iteración 3.1, la iteración 3 solo tiene que devolver la suma de los numeros en un array y ya lo tenías bien, es el 3.1 sum() donde se pide la comprobación.

}
sumResult = sumResult + numbersArray[i];
}
return sumResult;
}

// Iteration #3.1 Bonus:
function sum() {}


function sum(numbersArray) {
let newResult = 0;
for (i = 0; i < numbersArray.length; i++) {
if (typeof numbersArray[i] === 'string') {
newResult = newResult + numbersArray[i].length;
} else {
newResult = newResult + numbersArray[i];
}
}
return newResult;
}

// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}

function averageNumbers(numbersArray) {
let sumResult = 0;
if (numbersArray.length == 0) {
return null;
}
for (i = 0; i < numbersArray.length; i++) {
sumResult = sumResult + numbersArray[i];
}
let totalSum = sumResult / numbersArray.length;
return totalSum;
}

// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(wordsArray) {
let sumResult = 0;
if (wordsArray.length == 0) {
return null;
}
for (i = 0; i < wordsArray.length; i++) {
sumResult = sumResult + wordsArray[i].length;
}
let totalSum = sumResult / wordsArray.length;
return totalSum;
}

// Bonus - Iteration #4.1
function avg() {}

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
'poison',
'contagious',
'simple',
'bring',
'sharp',
'playground',
'poison',
'communion',
'simple',
'bring'
];

function uniquifyArray() {}
function avg(arr) {
if (arr.length == 0) {
return null;
}
let sumResult = 0;
for (i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string') {
sumResult = sumResult + arr[i].length;
} else {
sumResult = sumResult + arr[i];
}
}
let avgResult = sumResult / arr.length;
return Number(avgResult.toFixed(2));
}

// Iteration #5: Unique arrays

function uniquifyArray(someArray) {
if (someArray.length == 0) {
return null;
}
let clearArray = [];
for (let i = 0; i < someArray.length; i++) {
if (clearArray.indexOf(someArray[i]) === -1) {
clearArray.push(someArray[i]);
}
}
return clearArray;
}

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

function doesWordExist() {}


function doesWordExist(someArray, someword) {
if (someArray.length == 0) {
return null;
}
for (let i = 0; i < someArray.length; i++) {
if (someArray[i] === someword) {
return true;
}
}
return false;
}

// Iteration #7: Count repetition
const wordsCount = [
'machine',
'matter',
'subset',
'trouble',
'starting',
'matter',
'eating',
'matter',
'truth',
'disobedience',
'matter'
];

function howManyTimes() {}

function howManyTimes(someArray, someword) {
let times = 0;

for (let i = 0; i < someArray.length; i++) {
if (someArray[i] === someword) {
times = times + 1;
}
}
return times;
}

// Iteration #8: Bonus
const matrix = [
Expand Down Expand Up @@ -108,9 +170,6 @@ const matrix = [

function greatestProduct() {}




// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
Expand Down