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

[MAD PP Ángel Pérez y Esperanza Amaya] #515

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
71 changes: 71 additions & 0 deletions 71 starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
// Find the maximum
function maxOfTwoNumbers (a, b) {
if (a>b) {
return a;
} else if (b>a) {
return b;
} else {
return a;
}
}

// Finding Longest Word
var words = [
Expand All @@ -11,14 +20,43 @@ var words = [
'crackpot'
];

function findLongestWord (array) {
var longest = 0;
for (var i = 0; i < array.length; i++) {
if (array[i].length > longest) {
longest = array[i].length;
Copy link
Contributor

Choose a reason for hiding this comment

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

Os falla un pequeño detalla, en lugar de guardar la longitud del string si guardáis directamente el string y tambien tenéis el return dentro del for, si lo sacáis funcionaría.

var longest = array[0]; 
  for (var i = 1; i < array.length; i++) {
    if (array[i].length > longest.length) {
      longest = array[i];
    } 
  }
  return longest;

}
return longest;
}
}


// Calculating a Sum

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

function sumArray(array) {
if (array !== "") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Os falla que no estáis retornando nada cuando hacéis el reduce, os faltaría después del else: return result. y otro detalle, cuando estáis utilizando la propiedad reduce tenéis que darle un valor inicial. En total se os quedaría un código así.

  if (array !== "") {
  var result=array.reduce(function(a,b){
  return a + b;  
  },0)
} else {
  return 0;
}
return result;

var result=array.reduce(function(a,b){
return a + b;
})
} else {
return 0;
}
};

// Calculate the Average

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

function averageNumbers(array){
Copy link
Contributor

Choose a reason for hiding this comment

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

Os pasa lo mismo que en de arriba, también tenéis que controlar si el la variable result tiene valor para poder dividirla, porque sino os darían error.

var result=array.reduce(function(a,b){
a + b;
})
return result/array.length;
}


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

function averageWordLength(array) {
var result=array.reduce(function(a,b){
a.length + b.length;
})
return result/array.length;
}


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

function uniquifyArray(array) {
return (array.unique());
}



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

function doesWordExist(argument, array){
for (i=0; i<array.length; i++){
if (argument == array[i]) {
return true;
}
}
}


// Counting Repetion
var wordsCount = [
'machine',
Expand All @@ -74,6 +135,16 @@ var wordsCount = [
'disobedience',
'matter'
];

function howManyTimes(array, wordToSearch) {
var n=0;
for (i=0; i < array.length; i++) {
if(array[i] == wordToSearch) {
n++;
}
}
}

// Bonus Quest

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