Hi, I\'d like some help with the following. I have attached a word document that
ID: 3692856 • Letter: H
Question
Hi, I'd like some help with the following. I have attached a word document that asks for three javascript functions to be written. Thanks!
(i) Array Practice 1. Write a function named arrAvg that
accepts an array of numbers and returns their average.
(ii) Array Practice 2. Write a function named arrMax
that accepts an array of numbers and returns the largest
number in the array.
(iii) Array Practice 5. Write a function named
isMember that accepts two arguments—an array of strings and
a string—and returns true if the string is contained in the array,
false otherwise. You may solve this problem using a loop, or by
using the Array indexOf method.
Explanation / Answer
1)
//js1.htm
<!-- html program that demonstrates the function arrAvg-->
<html>
<body>
<script type="text/javascript" >
function arrAvg(array)
{
var len = array.length;
var total = 0;
for (var i in array) {
total += array[i];
}
alert("Average number is " + total/len);
}
//set array with numbers
var my_array = [1, 2, 3, 4];
//call function arrAvg that takes array and display a message
//with average of numbers
console.log(arrAvg(my_array));
</script>
</body>
</html>
------------------------------------------------------------------------------------------------------------
2)
//js2.htm
<!-- html program that demonstrates the function arrMax-->
<html>
<body>
<script type="text/javascript" >
function arrMax(array)
{
var len = array.length;
var max = array[0];
for (var i=1;i<len; i++) {
if(max<array[i])
max=array[i]
}
alert("Maximum number is " + max);
}
//set array with numbers
var my_array = [1, 2, 3, 4];
//call function arrMax that takes array and display a message
//with maximum of array numbers
console.log(arrMax(my_array));
</script>
</body>
</html>
------------------------------------------------------------------------------------------------------------
3)
//js3.htm
<!-- html program that demonstrates the function isMember-->
<html>
<body>
<script type="text/javascript" >
function isMember(array,string)
{
var len = array.length;
var found = false;
for (var i=0;i<len && !found; i++) {
if (array[i]==string)
found = true;
}
return found;
}
//set array with numbers
var my_array = ["c", "d","e", "f"];
//call function isMember that takes array and display a message
//with true if string contain in array,otherwise returns false
document.write(isMember(my_array, "e"));
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.