Question
This is the question Task 1: Play with numbers [10 marks] Using function declaration, define a JavaScript function (name it: playNumbers), which takes indefinite number
Task 1: Play with numbers [10 marks]
Using function declaration, define a JavaScript function (name it: playNumbers), which takes indefinite number of parameters. This function does not return any value. Instead, (1) if the function call didn't pass any arguments, your function displays:
There are no inputs.
program terminates.
(2) if one or more arguments are not numbers, your function displays: (The parameter) is not a number. for all the arguments that are not a number. e.g. if the parameters are ("abc", 3, "hello"), your program displays: abc is not a number.
hello is not a number.
program terminates.
(3) Otherwise (if not (1) nor (2)), (3.1) it displays the largest value of all the parameters in the format:
The largest number of (para1, para2,...) is (the largest value). e.g., The largest number of (9, 3, 6) is 9. NOTE: You need to write the logic to get the largest value. Do not use the built-in function.
(3.2) it displays the average of all the values in the format: The average of (para1, para2, ...) is (the average). e.g., The average of (9, 3, 6) is 6.
1
(4) Test your program using the following statements.
console.log("/***************************** Task1 *************************/");
playNumbers(); playNumbers("abc", 3, "hello"); playNumbers(9,3,6); playNumbers(3,5,1,3,5); playNumbers("Good day!",3,4,32,"hi", "we");
output is as follows:
/***************************** Task1 *************************/ There are no inputs. abc is not a number. hello is not a number.
The largest number of (9, 3, 6) is 9 The average of (9, 3, 6) is 6 The largest number of (3, 5, 1, 3, 5) is 5 The average of (3, 5, 1, 3, 5) is 3.4 Good day! is not a number. hi is not a number. we is not a number.
This is my work can you please solve this within my work I am getting different output
function playNumbers()
{
var count = arguments.length;
var total = 0;
for(i=0;i { total += count; } if(count == 0) { console.log("There are no inputs"); } console.log(total); var i; for(i=0;i { if (!Number(arguments[i])) { console.log(arguments[i] + " is not a number"); } } var largest = 0; var count = arguments.length; var i; for(i=0;i { if (arguments[i]>largest) { largest = arguments[i]; } } console.log(largest); var count = arguments.length; var total=0; var i=0; var avg=0; for(i=0;i { total+=arguments[i]; } avg=total/count; // return avg; console.log(avg); } playNumbers(); playNumbers("abc", 3, "hello"); playNumbers(9,3,6); playNumbers(3,5,1,3,5); playNumbers("Good day!",3,4,32,"hi", "we");
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started