Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

BCN - Edu - Paula - ES#464

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
balpaula wants to merge1 commit intoironhack-labs:masterfrombalpaula:master
Closed
Changes fromall 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
77 changes: 77 additions & 0 deletionsstarter-code/src/functions-and-arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
// Find the maximum
function maxOfTwoNumbers(num1, num2){
var max = num2;
if (num1>num2){
max = num1;
}
return max;
}


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

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

Choose a reason for hiding this comment

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

Es un poco mas leíble si en vez de guardar el índice guardas el valor y devuelves la string directamente.... pero está ok jejej 👍

Arkhanne reacted with laugh emoji
}
}
return array[index];
}


// Calculating a Sum

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

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

// Calculate the Average

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

function averageNumbers(array){
var suma = 0;
var lengthArray = array.length;
var result;
for (var i = 0; i < lengthArray; i++){
suma += array[i];
}
return lengthArray === 0 ? undefined : suma/lengthArray;
}

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

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

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

function uniquifyArray(array){
var newArray;
if (array.length > 0){
newArray = [];
for (var i = 0; i < array.length; i++){
if (!newArray.includes(array[i])){
newArray.push(array[i]);
}
}
}
return newArray;
}

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

function doesWordExist(array, word){
return array.includes(word);
}

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

function howManyTimes(array, word){
var counter = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

puedes añadir al principio:

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

Y así evitas ir al forEach innecesariamente.

Choose a reason for hiding this comment

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

Pero solo debería haber un return, no?

array.forEach(function(currentValue){
if (currentValue === word){
counter++;
}
});
return array.length === 0 ? false : counter;
}

// Bonus Quest

var matrix = [
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp