Try to write all of the exercises using both the for loop and while
loop.
-
Write a function
sumthat computes the sum of the numbers in an array. -
Write a function
maxthat accepts an array of numbers and returns the largest number in the array. -
Try the following at a console:
"the quick brown fox jumped over the lazy dog".split(" ");
"Hello, world!".split("")
"1,2,3,4,5,6".split(",")What is returned by split (You can read more about it
here),
and how does it work?
Use split to write a function longestWord that takes a string as an
argument and returns the longest word.
-
Write a function
removethat accepts an array and an element, and returns an array with all ocurrences of element removed.function remove(array, element) { // your code here } remove([1, 3, 6, 2, 3], 3); // => [1, 6, 2]
-
Write a function
evensthat accepts an array as an argument, and returns an array consisting of all of the even numbers in that array.
-
Write a function called
averagethat takes an array of numbers as a parameter and returns the average of those numbers. -
Write a function called
minthat finds the smallest number in an array of numbers. -
Write a function
shortestWordthat works likelongestWord, but returns the shortest word instead. -
Write a function
countCharthat takes two arguments: any string, and a character (string of one letter), and returns the number of times that the character occurs in the string. -
Write a function
evenLengthWordsthat takes an array of strings as an argument, and returns an array of just the words that have an even length.
-
Read about the
joinmethod on MDN and use it to implement a function that accepts a string as an argument and returns that string reversed. -
Write a function
keepthat "keeps" certain elements in an array. The function will need to take two arguments, an array, and something else -- the second argument will be what is used to determine which elements to keep.You should be able to use this function to write
evens,evenLengthWords, a hypotheticaloddsfunction, oroddLengthWordswithout changing thekeepfunction.