Skip to content

lab done #4242

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

console.log(maxOfTwoNumbers(100, 20))


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

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

let empty = ["apple"]
let result = findLongestWord(empty)
console.log(result)



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

function sumNumbers() {}

function sumNumbers(arr) {
let addition = 0
for (let i = 0; i<arr.length; i++){
addition += arr[i]
}
return addition
}
console.log(sumNumbers(numbers))


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

const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
// should return: 57

function sum(arr) {
let addition = 0
for (let i = 0; i<arr.length; i++){
if (typeof arr[i] === 'number'){
addition += arr[i]
} else if (typeof arr[i] === 'boolean') {
addition += arr[i]
} else if (typeof arr[i] === 'string') {
addition += arr[i].length
} else {
throw new Error('Error message goes here');
}
}
return addition
}

console.log(mixedArr)



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

let emptyAvg = []
console.log(avg(empty))
console.log(avg(numbersAvg))


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

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

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

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

// should return: 5.7

function avg(arr) {
if (arr.length === 0){return null}
let addition = 0
for (let i = 0; i<arr.length; i++){
if (typeof arr[i] === 'number'){
addition += arr[i]
} else if (typeof arr[i] === 'boolean') {
addition += arr[i]
} else if (typeof arr[i] === 'string') {
addition += arr[i].length
} else {
throw new Error('Error message goes here');
}
}
return addition / arr.length
}

console.log(mixedArr2)





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

function uniquifyArray() {}

function uniquifyArray(arr) {
if (arr.length === 0){
return null;
} else {
let uniqueWords = [arr[0]];
for( let i = 0; i< arr.length; i++){
if(uniqueWords.includes(arr[i])){
continue
} else{
uniqueWords.push(arr[i])
}
}
return uniqueWords
}
}
console.log(uniquifyArray(wordsUnique))


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

function doesWordExist() {}
function doesWordExist(arr, word) {
if (arr.length === 0){
return null
} else {
return arr.includes(word);
}
}

console.log(doesWordExist(wordsFind, 'dog'))



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

function howManyTimes() {}
function howManyTimes(arr, word) {
if (arr.length === 0){
return 0;
} else {
let counter = 0 ;
arr.forEach((item) => {
if (item === word){
counter += 1
}
})
return counter
}
}

console.log(howManyTimes(wordsCount, 'matter'))




Expand Down