Skip to content
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

Solved lab #426

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
72 changes: 67 additions & 5 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,96 @@
// 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 the Longest Word
const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"];

function findLongestWord() {}
function findLongestWord(arr) {
let indexOfLongest = -1 ;
let lengthOfLongest = 0 ;
for(i=0 ; i < arr.length ; i++ ){
if(arr[i].length > lengthOfLongest){
lengthOfLongest = arr[i].length
indexOfLongest = i
}
}

if (indexOfLongest === -1){
return null ;
}
else {
return arr[indexOfLongest];
}

}




// Iteration 3 | Sum Numbers
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

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




// Iteration 4 | Numbers Average
const numbers2 = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(arr) {
if (arr.length !== 0 && Array.isArray(arr) ) {
return sumNumbers(arr) / arr.length;
}
else {
return 0 ;
}


}




// Iteration 5 | Find Elements
const words2 = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"];

function doesWordExist() {}
function doesWordExist(arr, word) {
let isExists = false ;
if (arr.length !== 0 && Array.isArray(arr) ) {
for(i=0; i < arr.length ; i++) {
if( arr[i] === word ){
isExists = true ;
break ;
}
}

}
else {
isExists = null ;
}
return isExists;
}



// arr.forEach((item,word)=>{ if (item === word) { isExists = true; } else { isExists = false; } } )