Skip to content

Emiliano.Arreola.mex.wd-pt.apr-23 #3780

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
165 changes: 130 additions & 35 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,179 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
if (a>b)
return a;
else if (a<b)
return b;
else (a==b)
return a || b
}
maxOfTwoNumbers();




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

function findLongestWord() {}
function findLongestWord(arr) {
let x = "";
if (arr.length === 0) return null;
else {
for (let i = 0; i < arr.length; i++) {
if (x.length < arr[i].length) {
x = arr[i];
}
}
}
return x;
}


findLongestWord();




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

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


sumNumbers();

// Iteration #3.1 Bonus:
function sum() {}
function sum(arr) {
let x = 0
if ( x===0) {
return 0
}
else
for (let i=0; i<arr.length; i++) {
sum += arr [i]
}
return sum;
}

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(arr) {
let avg = 0
if (arr.length === 0 ) {
return null }
else {
for (i=0; i< arr.length; i++){
avg += arr[i];

}
return avg/arr.length

}
}

averageNumbers();


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

function averageWordLength() { }
function averageWordLength(arr) {
let avg = 0
if (arr.length === 0 ) {
return null }
else {
for (i=0; i< arr.length; i++){

Choose a reason for hiding this comment

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

nice movee 🔥

avg += arr[i].length;
}
return avg/arr.length

}
}
averageWordLength();



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

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

function uniquifyArray() {}
function uniquifyArray(arr) {

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



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

function doesWordExist(arr, bob) {
if (arr.length > 0) {

Choose a reason for hiding this comment

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

not a blocker but where is bob comming from ?

return arr.includes(bob);
} else {
return null;
}
}
doesWordExist();

function doesWordExist() {}



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

Expand All
const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(arr, bob) {

Choose a reason for hiding this comment

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

same here

let times = 0;

for (i = 0; i < arr.length; i++) {
if (arr[i] === bob) {
times++;
}
}
return times
}
howManyTimes();


// Iteration #8: Bonus



Expand Down