- Notifications
You must be signed in to change notification settings - Fork0
Sureshvarma4599/lab-javascript-functions-and-arrays
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Manipulating arrays is a very common operation we perform while programming. Whether you're calculating the total order value for a shopping cart, grabbing only the first names out of a list, or moving a piece on a chessboard, you're going to be modifying or manipulating an array to get the result.
In a similar fashion, you are gonna perform a few array & function related operations in this lab. Go to the src/functions-and-arrays.js file and complete all the unfinished code to complete this challenge.You can see the output in browser console
Fork this repoClone this repoPractice JavaScript - Arrays
Upon completion, run the following commands:git add .git commit -m "ProGrad ID"git push origin masterAnd finally, create a pull request so your ProGrad Mentor (PM) can review your work.
Open theSpecRunner.html
file on your browser and start coding to pass the test. Remember to focus on one test at a time and read carefully the instructions to understand what should be done.
Define a functiongreatestOfTwoNumbers
that takes two numbers as arguments and returns the greatest number.
Declare a function namedfindScaryWord
that takes as argument an array of names and returns the name which contains the maximum characters. If there are 2 names with the same number of characters, it should return the first occurence with max characters.
You can use the following array to test your solution:
constwords=['George','Alice','Alex','John','Infanta','Xavior','LourdhAntony'];
Calculating a sum can be as simple as iterating over an array and adding each of the elements.
John spent his money on buying groceries, he wants to find the net amount that he had spent in the super market.
Declare a function namednetPrice
that takes an array of prices as an argument, and returns the sum of all items in the array. Later in the course we'll learn how to do this by using thereduce
array method, which will make your work significantly easier. For now, let's practice"declarative" way - adding values, using loops.
You can use the following array to test your solution:
constprices=[200,120,100,108,135,162,25,170,80,110];
The goal: Learn how to refactor your code. :
In progression 3, you created a function that returns the sum of an array of prices. But what if we wanted to know how much is the sum of the length of all of the words in an array? What if we wanted to addboolean values to the mix? We wouldn't be able to use the same function as above, or better saying, we would have totweak it a little bit so that it can be reused no matter what is in the array that is passed as argument when functionsumOfArray()
is called.
Here we're applying a concept we callpolymorphism, that is, dealing with a functions' input independent of the types of data passed.
Let's create a new functionadd()
that calculates the sum for array filled with (almost) any type of data. Note that strings should have their length added to the total, and boolean values should be coerced into their corresponding numeric values. Check the tests for more details.
You can use the following array to test your solution:
constmixedArr=[63,122,'audi',61,true,'volvo','20','lamborghini',38,156];// should return: 463
Calculating the mid point is an extremely common operation. Let's practice it a bit.
The logic behind this:
- Find the sum as we did in the first exercise (or how about reusing that thesumOfArray()?)
- Take that sum and divide it by the number of elements in the list.
Consider a Surveyor is leveling a road and making notes of it. He wants to find the mid point of all those levels.
Declare a function namedmidPointOfLevels
that expects an array of numbers and returns the average of the numbers:
Starter Code
constlevels=[22,16,9,10,7,14,11,9];
Declare a function namedaverageWordLength
that receives as a single argument an array of words and returns the average length of the words:
Starter Code
constitems=['bread','jam','milk','egg','flour','oil','rice','coffee powder','sugar','salt'];
Create functionavg(arr)
that receives any mixed array and calculates average. Consider as mixed array an array filled with numbers and/or strings and/or booleans. We're following a similar logic to the one applied on the bonus iteration 4.1 😉
constmixedArr=[63,122,'audi',61,true,'volvo','20','lamborghini',38,156];// should return: 46.3
Take the following array, remove the duplicates, and return a new array. Check out theindexOf
Array method.
Do this in the form of a functionuniqueArray
that receives an array of words as a argument.
Starter Code
constitems=['bread','jam','milk','egg','flour','oil','rice','coffee powder','sugar','salt','egg','flour'];
Let's create a simple array search.
Declare a function namedsearchElement
that will take in an array of words as one argument, and a word to search for as the other. Returntrue
if it exists, otherwise, returnfalse
.Don't useindexOf
for this one.
Starter Code
constwords=['door','window','ceiling','roof','plinth','tiles','ceiling','flooring'];
Declare a function namedhowManyTimesElementRepeated
that will take in an array of words as the first argument, and a word to search for as the second argument. The function will return the number of times that word appears in the array.
Starter Code
constwords=['machine','matter','subset','trouble','starting','matter','eating','matter','truth','disobedience','matter'];
What is the greatest product of four adjacent numbers? Any four numbers that are next to each other horizontally or vertically are considered adjacent.
For example, if we have a 5x5 Matrix like:
[ 1, 2, 3, 4, 5][ 1, 25, 3, 4, 5][ 1, 20, 3, 4, 5][ 1, 20, 3, 4, 5][ 1, 4, 3, 4, 5]
The greatest product will be the25
x20
x20
x4
=40000
;
Declare a function namedmaximumProduct(matrix)
to find it in the 10x10 grid below!
constmatrix=[[08,02,22,97,38,15,00,40,00,75],[49,49,99,40,17,81,18,57,60,87],[81,49,31,73,55,79,14,29,93,71],[52,70,95,23,04,60,11,42,69,24],[22,31,16,71,51,67,63,89,41,92],[24,47,32,60,99,03,45,02,44,75],[32,98,81,28,64,23,67,10,26,38],[67,26,20,68,02,62,12,20,95,63],[24,55,58,05,66,73,99,26,97,17],[21,36,23,09,75,00,76,44,20,45]];
Following the logic you've used in iteration #8, declare a function calledmaximumProductOfDiagonals(matrix)
. It takes a matrix as a parameter and returns the greatest product of any four values layed out diagonally, in either direction.
Happy Coding ProGrad ❤️
About
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript99.6%
- HTML0.4%