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

Albert-Farre-Corbera#383

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
xapi1972 wants to merge1 commit intoironhack-labs:masterfromxapi1972: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
172 changes: 172 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,11 @@
// Find the maximum
function maxOfTwoNumbers(number1,number2) {
if (number1>number2) {return number1;}
if (number2>number1) {return number2;}
return number1; // if non of the other conditions are applied, it means both number are equal
}

console.log(maxOfTwoNumbers(15,10));

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



function findLongestWord(stringArray) {
// Create a new array with the length of each word, using a callback function
var arrayLong=stringArray.map(function(num) {return num.length});
// We get the largest number of characters
var largestNumber=Math.max(...arrayLong);
// We find the posisition of the largest length. If there are occurrences with the same length, indexOf returns the first position of the array
var positionLargest = arrayLong.indexOf(largestNumber);
return stringArray[positionLargest];
}

console.log(findLongestWord(words));

// Calculating a Sum

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

function sumArray(arrayToSum) {
if (arrayToSum.length!=0) {
var sumaValoresArray=arrayToSum.reduce(function (total, currentValue, currentIndex, arr) {return total+currentValue;});
return sumaValoresArray;
} else {
return 0;
}

}

console.log(sumArray(numbers));

// Calculate the Average

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

function averageNumbers(numberArray) {
if (numberArray.length===0) {
return undefined;
}
var sumaValuesArray = sumArray(numberArray);
var averageValue = sumaValuesArray/numberArray.length;
return averageValue;

}

console.log(averageNumbers(numbersAvg));

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

function averageWordLength(stringArray){
// Create a new array with the length of each word, using a callback function
var arrayLong=stringArray.map(function(num) {return num.length});
// We have now a number array, so we can call the function averageNumbers
var averageWordLength = averageNumbers(arrayLong);
return averageWordLength;
}

console.log(averageWordLength(wordsArr));

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

function uniquifyArray(wordArray) {
var k=0;
var newArray= [];

if (wordArray.length===0) {
return undefined;
}

for (i=0;i<wordArray.length;i++) {
if (newArray.indexOf(wordArray[i])===-1) {
newArray[k]=wordArray[i];
k=k+1;
}
}
return newArray;


}

console.log(uniquifyArray(wordsUnique));

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

function doesWordExist(wordArray,wordToSearch) {
var exist= wordArray.find(function(element) {return element===wordToSearch});
if (exist!=undefined) {
return true;
} else {
return false;
}
}

console.log(doesWordExist(wordsFind,'truth'));

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

function howManyTimes(arrayToSearchIn,wordToSearch) {
var counter = 0;

if (arrayToSearchIn.length===0) {
return false;
}
arrayToSearchIn.forEach(function (element) { if (element === wordToSearch) {counter++}});
return counter;
}

console.log(howManyTimes(wordsCount,'matter'));

// Bonus Quest

var matrix = [
Expand All@@ -98,3 +198,75 @@ 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]
];

var newMatrix= [];
var newMatrix1=[];


for (i=0;i<20;i++) {
newMatrix.push(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ojo con los nombres de las propiedades, es aconsejable especificar mejor que contienen o para que se van a utilizar. Por ejemplo, 'cards' o 'numbers'

}
console.log(newMatrix);
for (j=0;j<20;j++) {
newMatrix1.push(newMatrix);
console.log(newMatrix1[j].length)
}

// Cartesian product of 4 consecutive positions of the matrix
// We program to be able to multiply any adjacent numbers specified in variable consecutiveNumbers

function greatestProduct(matrixToCalculate) {

var consecutiveNumbers = 4;
var greatestProductInMatrix=0;

// we start calculating the cartesian product for each row in the matrix
var arrayTotalResult1= calculateCartesianProductMatrix(matrixToCalculate,consecutiveNumbers);

// We transpond rows and cols of the matrixToCalculate
var matrixTransponded=transpondRowsColsMatrix(matrixToCalculate);
// We calculate the cartesian product for the transponded matrix and push de array result to the existing arrayTotalResult
var arrayTotalResult2=calculateCartesianProductMatrix(matrixTransponded,consecutiveNumbers);
console.log(arrayTotalResult1);

var arrayTotalResult=arrayTotalResult1.concat(arrayTotalResult2);

largestNumber=Math.max(...arrayTotalResult);

return largestNumber;

}

function calculateCartesianProductMatrix(matrixEvaluate,adjacentNumbers) {

var arrayResult = []; // we store in this array the different result of cartesian product.


matrixEvaluate.forEach(function(element){
for (i=0;i<element.length-adjacentNumbers;i++) {
var productItem=element.slice(i,i+adjacentNumbers).reduce(function(totalProduct,itemVectorSlice){
return totalProduct*itemVectorSlice;
});
arrayResult.push(productItem);
}

});

return arrayResult;
}

function transpondRowsColsMatrix(matrixToTranspond){

// We transpond the rows and cols of the matrix. Every col is now a row, every row is a col
var transpondedArray=[];
for (col=0;col<matrixToTranspond.length;col++){
var colArray=[];
for (row=0;row<matrixToTranspond.length;row++) {
colArray.push(matrixToTranspond[row][col]);
}
transpondedArray.push(colArray);
}
return transpondedArray;
}

console.log("the largest value is: ",greatestProduct(matrix));

[8]ページ先頭

©2009-2025 Movatter.jp