Skip to content

yasoli06 "solved lab" #4156

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
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
161 changes: 134 additions & 27 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,121 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers(num1,num2) {
if (num1 > num2) {
return num1;
} else if (num1 < num2){
return num2;
} else {
return num1;
}
}

// 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;
}
for (let i = 0; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
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;
}
for (let i = 0; i < numbers.length; i++){
sum += numbers[i];
}
return (sum);
}

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



const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];


function sum(mixedArr) {
let sum = 0;
if (mixedArr.length === 0) {
return 0;
}
for (let i = 0; i < mixedArr.length; i++) {
if (typeof mixedArr[i] === "number") {
sum += mixedArr[i];
} else if (typeof mixedArr[i] === "string"){
sum += mixedArr[i].length;
} else if (typeof mixedArr[i] === "boolean"){
if (mixedArr[i] === true) {
sum += 1;
} else {
sum += 0;
}
} else {
throw new Error("That value is not a number, string o boolean");
}
}
return 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(numbersAvg) {
let sum = 0;
if (numbersAvg.length === 0) {
return null;
}
for (let i = 0; i < numbersAvg.length; i++){
sum += numbersAvg[i];
}
return sum / numbersAvg.length;
}

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

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

// Bonus - Iteration #4.1
function avg() {}
const arr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
function avg(arr) {
let sum = 0;
if (arr.length === 0) {
return null;
}
for (let i = 0; i < arr.length ; i++){
if (typeof arr[i] === "number") {
sum += arr[i];
} else if (typeof arr[i] === "string"){
sum += arr[i].length;
} else if (typeof arr[i] === "boolean"){
if (arr[i] === true) {
sum += 1;
} else {
sum += 0;
}
}
}
return sum / arr.length ;
}

// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,16 +132,34 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}


function uniquifyArray(wordsUniqu) {
let newArr = [];
if (wordsUniqu.length === 0) {
return null;
}
for (let i = 0; i < wordsUniqu.length; i++) {
if (!newArr.includes(wordsUniqu[i])) {
newArr.push(wordsUniqu[i]);
}
}
return newArr;
}

// 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;
}
for (let i = 0; i < wordsFind.length; i++){
if (wordsFind.includes(word)){ // perguntar pq no puedo poner wordsFind[i], como en el caso anterior. ?!
return true;
} else {
return false;
}
}
}

// Iteration #7: Count repetition
const wordsCount = [
Expand All @@ -78,10 +176,19 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}



function howManyTimes(wordsCount, words) {
let newCount = 0;
if (wordsCount.length === 0){
return 0;
} else {
for (let i = 0; i < wordsCount.length; i++){
if (wordsCount[i] == words) {
newCount++;
}
}
return newCount;
}
}
// Iteration #8: Bonus
const matrix = [
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
Expand Down