Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Ber_Svenja #738

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 6 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion 2 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Define a function `maxOfTwoNumbers` that takes two numbers as arguments and retu

## Finding Longest Word

Write a function `findLongestWord` that takes an array of words and returns the length of the longest one. If there are 2 with the same length, it should return the first occurrence.
Write a function `findLongestWord` that takes an array of words and returns the longest one. If there are 2 with the same length, it should return the first occurrence.

**Starter Code**

Expand Down
113 changes: 112 additions & 1 deletion 113 starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@

// Find the maximum
function maxOfTwoNumbers(a,b){
if (a > b){
return a;
}
else {
return b;
}
}


maxOfTwoNumbers(998,89);
// Finding Longest Word
var words = [
'mystery',
Expand All @@ -11,15 +22,64 @@ var words = [
'crackpot'
];

// Calculating a Sum
function findLongestWord(words){
var sortedword = words.sort(function (a,b){
return b.length - a.length;
})
return sortedword[0]
}
console.log(findLongestWord(words))


// Calculating a Sum
/*
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

var reducer = (accumulator, currentValue) => accumulator + currentValue;

console.log(numbers.reduce(reducer));
*/

function sumArray (array){
curRes=0;
for(var i= 0;i<array.length;i++){
curRes += array[i];
}
return curRes;
}

// Calculate the Average

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

function averageNumbers(array){
if(array.length === 0){
return undefined;
}

curRes=0;
for(var i= 0; i < array.length; i++){
curRes += array[i];
}
curRes /= array.length;
return curRes;
}
console.log(averageNumbers(numbersAvg))

// Array of Strings

function averageWordLength(array){
if(array.length === 0){
return undefined;
}
curLeng = 0;
for(var i = 0; i < array.length; i++){
curLeng += array[i].length;
}
curLeng /= array.length;
return curLeng
}

var wordsArr = [
'seat',
'correspond',
Expand All @@ -34,6 +94,22 @@ var wordsArr = [
];

// Unique Arrays

function uniquifyArray(array){
if(array.length === 0){
return undefined;
}
var curRes = [];
for(var i = 0; i < array.length; i++){
//if(array.indexOf(array[i]) === array.indexOf(array)){}
//array.search(array[i])
if(curRes.indexOf(array[i]) === -1){
curRes.push(array[i])
}
}
return curRes
};

var wordsUnique = [
'crab',
'poison',
Expand All @@ -48,7 +124,24 @@ var wordsUnique = [
'bring'
];

console.log(uniquifyArray(wordsUnique));


// Finding Elements
function doesWordExist(array, word){
if(array.length === 0){
return false;
}
let checker = false;
for(var i = 0; i < array.length; i++){

if (array[i] === word){
checker = true
}
}
return checker
};

var wordsFind = [
'machine',
'subset',
Expand All @@ -60,7 +153,21 @@ var wordsFind = [
'disobedience'
];


// Counting Repetion
function howManyTimes(array, word){
if(array.length === 0){
return false;
}
var counter = 0;
for(var i= 0; i < array.length; i++){
if(array[i] === word){
counter++
}
}
return counter;
};

var wordsCount = [
'machine',
'matter',
Expand All @@ -76,6 +183,9 @@ var wordsCount = [
];
// Bonus Quest

function greatestProduct(matrix){
// for(var i = 0;i<matrix.length)
};
var matrix = [
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
Expand All @@ -98,3 +208,4 @@ var matrix = [
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

2 changes: 1 addition & 1 deletion 2 starter-code/tests/FunctionsAndArraysSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('Counting Repetion - howManyTimes', function () {
});

it('returns false with an empty array', function () {
expect(howManyTimes([])).toBe(undefined);
expect(howManyTimes([])).toBe(false);
});

it('returns one when the word appears only one time on the array', function () {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.