Skip to content

Néboa - lab-javascript-functions-and-arrays #3839

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 2 commits into
base: master
Choose a base branch
from
Open
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
158 changes: 147 additions & 11 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,141 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(n1,n2) {
if (n1 > n2){
return n1;
} else {
return n2;
}
}



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

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

} else {
for (let i=0; i < words.length; i++){
let word = words[i];
if (!longestWord || longestWord.length < word.length){
longestWord = word;
}
}
return longestWord;
}
}



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

function sumNumbers() {}
function sumNumbers(numbers) {
let sum = 0
if (numbers.length === 0){
return 0;
} else {
for (let i = 0; i<numbers.length; i++){
let number = numbers[i]
sum += number;
}
}
return sum
Comment on lines +37 to +46

Choose a reason for hiding this comment

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

Suggested change
let sum = 0
if (numbers.length === 0){
return 0;
} else {
for (let i = 0; i<numbers.length; i++){
let number = numbers[i]
sum += number;
}
}
return sum
if (numbers.length === 0){
return 0;
}
return sum(numbers);

Ya que te has hecho el metodo sum! pues vamos a usarlo! :D


}



// Iteration #3.1 Bonus:
function sum() {}
function sum(mixedArr) {
let countChar = 0;
let countNum = 0;
let countBol = 0;

if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
let sumNumChar = countChar + countNum+ countBol;
return sumNumChar;
Comment on lines +54 to +75

Choose a reason for hiding this comment

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

Suggested change
let countChar = 0;
let countNum = 0;
let countBol = 0;
if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
let sumNumChar = countChar + countNum+ countBol;
return sumNumChar;
let count = 0;
if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number' || typeof element === 'boolean') {
count += element;
} else if (typeof element === 'string') {
count += element.length;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
return count;

Bien!, entiendo perfectamente lo de las 3 variables, pero fijate la propia definicion del metodo te dice que es una suma, es decir que todo tendra que ser un numero!, por ello, si te creas una unica variable que sea de contador lo tendrias, ahora bien, veras que he puesto los numeros y los booleanos juntos, esto es porque si tu intentas sumar un booleano hace lo siguiente, true = 1 y false = 0, por ello si intentas hacer const x = 1 + true, x valdria 2! :D

}



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

function averageNumbers() {}
function averageNumbers(numbersAvg) {
let sum = 0
if (numbersAvg.length === 0){
return null;
} else {
for (let i = 0; i < numbersAvg.length; i++) {
let number = numbersAvg[i];
sum += number;
}
}

return sum / numbersAvg.length;
Comment on lines +85 to +95

Choose a reason for hiding this comment

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

Viendo las correcciones que te he puesto antes con la clase sum, este fragmento se podria hacer de otra forma no? mmmmm como lo harias???

}


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

function averageWordLength() { }
function averageWordLength(wordsArr) {
let count = 0;
if (wordsArr.length === 0){
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let char = wordsArr[i];
count += char.length;
}
}
return count / wordsArr.length;
Comment on lines +103 to +112

Choose a reason for hiding this comment

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

Con este fragmento de codigo me pasa lo mismo!, esto se parece al metodo sum no? xD

}


// Bonus - Iteration #4.1
function avg() {}
function avg(wordsArr) {
let countChar = 0;
let countNum = 0;
let countBol = 0;

if (wordsArr.length === 0) {
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let element = wordsArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
}
}
}
let sumNumChar = countChar + countNum + countBol;
return sumNumChar / wordsArr.length;
Comment on lines +118 to +137

Choose a reason for hiding this comment

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

Suggested change
let countChar = 0;
let countNum = 0;
let countBol = 0;
if (wordsArr.length === 0) {
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let element = wordsArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
}
}
}
let sumNumChar = countChar + countNum + countBol;
return sumNumChar / wordsArr.length;
return sum(wordsArr)/ wordsArr.length;

Fijate!, has repetido todo el codigo que estaba en sum, pues entonces para ver la potencia de las funciones ejecutamos sum, repito, ejecutamos pasandole como argumento el array y listo!, tendriamos lo mismo!!! :D

}

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

function uniquifyArray() {}

function uniquifyArray(wordsUnique){

let finalArray = [ ];

if (wordsUnique.length === 0){
return null;
} else {
for (let i=0; i<wordsUnique.length; i++){
const word = wordsUnique[i];
if (!finalArray.includes(word)){
finalArray.push(word);
}
}
}
return finalArray;
}



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

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



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

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



Expand Down