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

[MAD PP] Juan Sanchez & Mateus Felix#261

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
thebinaryfelix wants to merge2 commits intoironhack-labs:masterfromthebinaryfelix: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
198 changes: 198 additions & 0 deletionsstarter-code/src/functions-and-arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
// Find the maximum

function maxOfTwoNumbers(a, b){
if(a > b) {
return a;
} else {
return b;
}
}

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

function findLongestWord(array){
if(array == '' ){
return undefined;
} else {
var longestWord = "";
for(var i=0; i<array.length; i++) {
if(array[i].length > longestWord.length) {
longestWord = array[i];
}
}
return longestWord;
}

}

// Calculating a Sum

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

function sumArray(array) {
if(array == '') {
return 0;
} else if(array.length == 1){
return array[0];
} else {
var total = array.reduce(function(a,b){
return a+b;
});
return total;
}

}

// Calculate the Average

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

function averageNumbers(array) {
if(array == '') {
return undefined;
} else {
return sumArray(array) / array.length;
}

}

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

function averageWordLength(array){
var totalLength = 0;

if(array == ''){
return undefined;
}
else{
for(var i=0 ; i<array.length ; i++){
totalLength += array[i].length;
}

return totalLength / array.length;
}
}

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

function uniquifyArray(array){

var newArray = [];

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

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

function doesWordExist(array, word){
if(array == ''){
return false;
}
else{
for(var i=0 ; i<array.length ; i++){
if(array[i] == word){
return true;
}
}
return false;
}
}

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

function howManyTimes(array, word){
if(array == ''){
return false;
}
else{
var count = 0;

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

return count;
}
}

// Bonus Quest

function greatestProduct(array){

var totalProduct = 0;
var product = 0;

var maxIndex = array.length;

for(var i = 0 ; i<maxIndex ; i++){
for(var j = 0 ; j<maxIndex ; j++){

//Position [0][0]
if(i==0 && j==0){
product = array[i+1][j] * array[i][j+1];
if(totalProduct < product){
totalProduct = product;
}
}
//Position [0][19]
else if(i==0 && j==maxIndex-1){
product = array[i+1][j] * array[i][j-1];
if(totalProduct < product){
totalProduct = product;
}
}
//Position [19][19]
else if(i==maxIndex-1 && j==maxIndex-1){
product = array[i-1][j] * array[i][j-1];
if(totalProduct < product){
totalProduct = product;
}
}
//Position [19][0]
else if(i==maxIndex-1 && j==0){
product = array[i-1][j] * array[i][j+1];
if(totalProduct < product){
totalProduct = product;
}
}
//Column on [i changing][0]
else if(j==0 && i!==0 && i!==maxIndex-1){
product = array[i+1][j] * array[i][j-1] * array[i][j+1];
if(totalProduct < product){
totalProduct = product;
}
}
//Row on [0][j changing]
else if(i==0 && j!==0 && j!==maxIndex-1){
product = array[i+1][j] * array[i][j+1] * array[i][j-1];
if(totalProduct < product){
totalProduct = product;
}
}
//Column on [i changing][19]
else if(j==maxIndex-1 && i!==0 && i!==maxIndex-1){
product = array[i+1][j] * array[i-1][j] * array[i][j-1];
if(totalProduct < product){
totalProduct = product;
}
}
//Row on [19][j changing]
else if(i==maxIndex-1 && j!==0 && j!==maxIndex-1){
product = array[i-1][j] * array[i][j-1] * array[i][j+1];
if(totalProduct < product){
totalProduct = product;
}
}
else{
product = array[i+1][j] * array[i-1][j] * array[i][j+1] * array[i][j-1];
if(totalProduct < product){
totalProduct = product;
}
}
}
}

return totalProduct;
}

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 +286,13 @@ 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]
];


//For testing
var matrix2 = [
[1, 1, 1, 1, 1],
[1, 1, 8, 1, 1],
[1, 8, 8, 8, 1],
[1, 1, 8, 1, 1],
[1, 1, 1, 1, 1],
]

[8]ページ先頭

©2009-2025 Movatter.jp