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

Paris Cécile et Régis #613

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
161 changes: 157 additions & 4 deletions 161 starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// Find the maximum

// Finding Longest Word

function maxOfTwoNumbers (num1, num2) {
if (num1 > num2) {

return num1;
}

else {

return num2;
}
}


// -----------------Finding Longest Word
var words = [
'mystery',
'brother',
Expand All @@ -11,15 +25,62 @@ var words = [
'crackpot'
];

// Calculating a Sum
function findLongestWord (array) {

var longestWord = array[0];

array.forEach (function(element) {

if(element.length > longestWord.length){

longestWord = element;
}
});
return longestWord;
}

findLongestWord (words);



// -----------------Calculating a Sum

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

// Calculate the Average
function sumArray (array) {
var theSum = 0;

array.forEach (function(element) {
theSum += element;
})
return theSum;
}







// -----------------Calculate the Average

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

// Array of Strings
function averageNumbers (array) {
var theSum = 0;
if(array.length === 0) {
return undefined;
}

else {
array.forEach (function(element) {
theSum += element;
})
}
return theSum/array.length;
}

// -----------------Array of Strings
var wordsArr = [
'seat',
'correspond',
Expand All @@ -33,6 +94,21 @@ var wordsArr = [
'palace'
];

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

else {
array.forEach (function(element) {
length += element.length;
})
}
return length/array.length;

}

// Unique Arrays
var wordsUnique = [
'crab',
Expand All @@ -48,6 +124,34 @@ var wordsUnique = [
'bring'
];


function uniquifyArray (array) {
var newArray = [];
if(array.length === 0) {
return undefined;
}

else {
newArray.push (array[0]);

for (var i=1; i< array.length; i++){

Choose a reason for hiding this comment

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

Using a for loop IN a for loop, is quite risky.. specially when you have a much more simple way to do it.

You can use 2 methods:

indexOf :
array.indexOf("Word") => will return the index of Word in the array if Word exists, or return -1 if the word doesn't exist. If it returns an index, you shouldnt push, if it returns -1 then you should push it.

Includes:
array.includes("Word") => will return TRUE if word exists, or FALSE if it doesnt exist. Push when its false.

example with the indexOf :

for (var i = 0; i < array.length; i++) {
    if (newArray.indexOf(array[i]) === -1) {
      newArray.push(array[i]);
    }
  }

Hope it's clear :)

for (var j=0; j<newArray.length; j++){
if (array[i] !== newArray[j]) {
newArray.push(array[i]);
}
}
}


}
if (newArray.length === array.length){
return array;
}
else{
return newArray;
}
}

// Finding Elements
var wordsFind = [
'machine',
Expand All @@ -60,6 +164,20 @@ var wordsFind = [
'disobedience'
];



function doesWordExist (array, word) {

var temp = false;

for (var i=0; i<array.length; i++) {
if (array[i] === word){
temp= true;
}
}
return temp;
}

// Counting Repetion
var wordsCount = [
'machine',
Expand All @@ -74,8 +192,43 @@ var wordsCount = [
'disobedience',
'matter'
];


function howManyTimes (array, word) {

var counter = 0;

if (array.length === 0){
return false;
}
else {
for (var i=0; i< array.length; i++) {
if (array[i] === word) {
counter ++;
}
}
}
return counter;
}
// Bonus Quest

function countUp (mat, x, y){
count = 0;
for (var i=0; i<4; i++) {
count *= mat[i][y];
}
return count;
}


function greatestProduct (array) {
var temp =0;



return temp;
}

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],

Choose a reason for hiding this comment

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

Here is the correction for the Matrix:

function greatestProduct(m) {
  var horizontal, vertical, diagonal;
  var greatProduct = 0;

  for (var i=0, nRows = m.length; i < nRows - 1; i++) {
    for (var j=0, nCols = m[0].length; j < nCols - 1; j++) {
      // 4 horizontal elements
      if (j < nCols-3) {
        horizontal = m[i][j] * m[i][j+1]   * m[i][j+2]   * m[i][j+3];
      }

      // 4 vertical elements
      if (i < nRows-3) {
        vertical   = m[i][j] * m[i+1][j]   * m[i+2][j]   * m[i+3][j];
      }

      // 4 Diagonal elements
      if (i < nRows-3 && j < nCols-3) {
        diagonal   = m[i][j] * m[i+1][j+1] * m[i+2][j+2] * m[i+3][j+3];
      }

      greatProduct = Math.max(greatProduct, horizontal, vertical, diagonal);
    }
  }

  return greatProduct;
}

Choose a reason for hiding this comment

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

It's globally well done! The code is clean and not too long. 💪

Keep up the good work!

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.