Skip to content

Ber wd pt Camille - complete #1472

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

Closed
wants to merge 1 commit into from
Closed
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
116 changes: 116 additions & 0 deletions starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
// Iteration #1: Find the maximum

function maxOfTwoNumbers (num1, num2){
console.log(Math.max(num1, num2))
}

maxOfTwoNumbers(4, 7)

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

function findLongestWord(myWords){
let longestWord = 0

for (let i = 0; i < myWords.length; i++){
if (myWords[i].length > longestWord){
longestWord = myWords[i]
}
return longestWord
//console.log(i)
//console.log(longestWord)
}
}

findLongestWord(words)

// Iteration #3: Calculate the sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

let result3 = 0

function sumArray(myNumber){
for (let i=0; i < myNumber.length; i++){
result3 += myNumber[i]
}
return result3
}
sumArray(numbers)
//console.log(result)

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


const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

let result4a = 0

function averageNumbers(myNumbersAvg){
for (let i=0; i < myNumbersAvg.length; i++){
result4a += myNumbersAvg[i] / myNumbersAvg.length
}
return result4a
}
averageNumbers(numbersAvg)

Copy link

Choose a reason for hiding this comment

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

nice job, also i could say as you see we have sumArray function and we could use it to get the average but its really good nice

// Level 2: Array of strings

const wordsArr = [
'seat',
'correspond',
Expand All @@ -25,6 +71,28 @@ const wordsArr = [
'palace'
];

let result4b = 0
let countLetters = []

function numberOfLetters(myWordsArr){
for (let i=0; i < myWordsArr.length; i++){
countLetters.push(myWordsArr[i].length)
}
return countLetters
}

numberOfLetters(wordsArr)


function averageWordLength(myCountLetters){
for (let i=0; i < myCountLetters.length; i++){
result4b += myCountLetters[i] / myCountLetters.length
}
return Math.floor(result4b)
}
averageWordLength(countLetters)


// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
Expand All @@ -40,6 +108,29 @@ const wordsUnique = [
'bring'
];


let result=[]

function check(array) {
for (let i=0; i < array.length; i++){
if (array.indexOf(array[i]) !== i){
result.push(array[i])
}
}
}
check(words)

function list(array) {
for (let i=0; i < array.length; i++){
console.log(array.indexOf(array[i]))
}
}
list(words)

console.log(result)



// Iteration #6: Find elements
const wordsFind = [
'machine',
Expand All @@ -52,6 +143,19 @@ const wordsFind = [
'disobedience'
];


function doesWordExist(array, check){
for(let i=0; i < array.length; i++){
if(check === array[i]){
return true;
} else {
return false;
}
}
}
doesWordExist(words2, "hello")


// Iteration #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -67,6 +171,18 @@ const wordsCount = [
'matter'
];

let numberofTimes = 0

function howManyTimes(array, check){
for(let i=0; i < array.length; i++){
if(check === array[i]){
numberofTimes++;
}
}
}

howManyTimes(words3, "matter")
console.log(numberofTimes)
// Iteration #8: Bonus

const matrix = [
Copy link

Choose a reason for hiding this comment

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

really good nice job

Expand Down