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

PAR Valérie Linarès & Shanshan Cao#414

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
moonchie wants to merge1 commit intoironhack-labs:masterfrommoonchie: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
59 changes: 58 additions & 1 deletionstarter-code/src/functions-and-arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
// Find the maximum
function maxOfTwoNumbers(num1, num2){
return Math.max(num1, num2);
};

maxOfTwoNumbers(2,3);

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

// Calculating a Sum
function findLongestWord(target){
var sorted = target.sort(
function(a, b){
return b.length - a.length;}
);
return sorted[0]
};

findLongestWord(words);


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

var sum = 0;
function sumArray(target){
var result = target.reduce(function(accumulator, currentValue, currentIndex, array) {
Copy link

@ta-web-parista-web-parisMay 31, 2018
edited
Loading

Choose a reason for hiding this comment

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

To resolve the error in jasmine just add this two lines of code :

if(target.length==0){sum=0returnsum;}

Because the jasmine want's you to deal with special case like this (empty array, ect..)

return accumulator + currentValue;
});
sum = result;
return sum;
}

sumArray(numbers)


// Calculate the Average

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

function averageNumbers(target){
sumArray(target); //get the sum

Choose a reason for hiding this comment

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

Same here you should add a condition, so when target is empty you return undefined

var len = target.length; //get the length
var average = sum / len; //get the average
return average;
}

averageNumbers(numbersAvg);

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

//console.log("sum starts at: " + sum);

function averageWordLength(target){
var numberArray = []; //create empty array
//console.log("numberArr starts at: "+ numberArray)

target.forEach(function (i){ //loop every element, get len, push to the numberArr
numberArray.push(i.length);
});
//console.log("numberArr ends at:" + numberArray);
sumArray(numberArray); //Update the sum value using the sum function
//console.log("sum ends at: " + sum);

averageNumbers(numberArray); //get average of numberArr
}

averageWordLength(wordsArr);


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



// Finding Elements
var wordsFind = [
'machine',
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp