Skip to content

feat: All iterations for lab functions and arrays js #3848

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/Clase_06_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//OBJETOS

const message = {
from: '666666666',
to: '6666555566'

}
135 changes: 125 additions & 10 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,112 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}

function maxOfTwoNumbers(num1, num2) {
let bigNumber;

if (num1 > num2) {
bigNumber = num1;
} else {
bigNumber = num2;
}

return bigNumber;
}


// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}
function findLongestWord(words) {
if (words.length === 0) {
return null;
}

if (words.length === 1) {
return words[0]
}

let longestWord = '';

for (let i = 0; i < words.length; i++) {
const findWord = words[i];

if (findWord.length > longestWord.length || longestWord === '') {
longestWord = findWord;
}

}

return longestWord;
}



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

function sumNumbers() {}
function sumNumbers(numbers) {

Choose a reason for hiding this comment

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

Suggested change
function sumNumbers(numbers) {
function sum(numbers) {

Para que no te falle, el nombre de las funciones tiene que ser el que te venia por defecto si no falla los test de jasmine jejeje

Choose a reason for hiding this comment

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

ahora mismo el codigo que tienes implementado solo suma numeros, es decir si te llega un string, o un array, o un objeto, o un booleano los intentas sumar todos, y eso no deberia ser asi, tienes que especificarlos un poco, te pongo como lo haria yo vale? y me dices cualquier cosa y lo vemos vale??

if (numbers.length === 0) {

Choose a reason for hiding this comment

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

Suggested change
if (numbers.length === 0) {
function sum(numbers) {
if (numbers.length === 0) {
return 0;
}
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
if (typeof numbers[i] === "string") {
sum += numbers[i].length;
} else if (typeof numbers[i] === "number" || typeof numbers[i] === "boolean") {
sum += numbers[i];
} else if (typeof numbers[i] === "object") {
throw new Error("Unsupported data type sir or ma'am");
}
}
return sum;
}

Choose a reason for hiding this comment

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

Esto que te he puesto nada me he equivocado, seria para la funcion sum, no para sumNumbers, para sumNumbers estaba genial, pero si lo piensas bien, sum y sumNumbers son iguales, solo que sum tiene vitaminas jejeje

return 0;
}

let sum = 0;

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

}
return sum;
}



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



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

function averageNumbers() {}
function averageNumbers(numbers) {
if (numbers.length === 0) {
return null;
}
let sum = 0;

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

const average = sum/ numbers.length;

Choose a reason for hiding this comment

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

Suggested change
const average = sum/ numbers.length;
const average = sum / numbers.length;

soy un pijo....

return average;
}


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

function averageWordLength() { }
function averageWordLength(words) {
if (words.length === 0) {
return null;
}

let totalLenght = 0;

for (let i = 0; i < words.length; i++) {
totalLenght += words[i].length;
}

const averageLength = totalLenght / words.length;
return averageLength;

}



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

Choose a reason for hiding this comment

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

Suggested change
//function avg() {}
function avg() {}

No te hace falta comentarlo, asi uno de los test no te falla jejeje


// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,14 +123,47 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
const uniqueArray = uniquifyArray(wordsUnique);


function uniquifyArray(wordsDuplicate) {

if (wordsDuplicate.length === 0) {
return null;
}

for (let i = 0; i < wordsDuplicate.length; i++) {
const word = wordsDuplicate[i];

while (wordsDuplicate.indexOf(word, i + 1) !== -1) {
wordsDuplicate.splice(wordsDuplicate.indexOf(word, i + 1), 1);
}
}

return wordsDuplicate;
}



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

function doesWordExist() {}
function doesWordExist(findElements, searchWord) {

if (findElements.length === 0) {
return null;
}

let i = 0

while (i < findElements.length) {
if (findElements[i] === searchWord) {
return true;
}
i++
}
return false;
}



Expand All @@ -78,7 +182,18 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
const wordSearch = 'matter'

function howManyTimes(wordsCount, wordSearch) {
let count = 0;
for (let i = 0; i < wordsCount.length; i++) {
const wordFind = wordsCount[i];
if (wordSearch === wordFind) {
count++
}
}
return count;
}



Expand Down