Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Create Performance Task

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Create Performance Task

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3a.

i.) The overall purpose of this program is to create an engaging, and interesting calculating
game that calculates the average of random numbers and shows whether the average is
positive or negative.

ii) One of the functionalities in the program is the function to calculate the average numbers in
an array which finds the average of the line of numbers the users provide, another function is
that there would be instructions saying “Enter numbers separated by commas:” which will
require the user to put in an input of random numbers. After the user puts in a random number
the program returns an output of the average number of the input the user has given and a
statement saying whether the average is a positive or negative number.

iii.) The input of the program is user input which is the integers the users give the program.

3b.)

i.) // Function to calculate the average of numbers in an array


function calculateAverage(num)
{
let sum = 0;
for (let i = 0; i < num.length; i++)
{
sum += num[i];
}
return sum / num.length;
}

ii.)
let input = prompt("Enter numbers separated by commas:");

iii.)
//Calculate average
let average = calculateAverage(num);
alert("The average of the numbers is: " + average.toFixed(2)); // Display average with two
decimal places

//Check for positive or negative number


if (average%2 == 0)
{
alert("This is a positive number");
}
else
{
alert("This is a negative number");
}
}

//main function to start the program


main();

iv.) The name of the list in this case is the input of random numbers we give separated by the
commas.

3c.)
i.)
//Function to calculate the average of numbers in an array
Function calculateAverage(num)
{

let sum = 0;
for (let i = 0; i < num.length; i++)
{
Sum += num[i];
}
return sum / num.length;
}

ii.) The provided function calculate average seems correct for calculating the average of
numbers in an array. It iterates through each element of the array, sums them up, and then
divides the sum by the length of the array to get the average.

iii.) Prompting User for Input: The main function starts by prompting the user to enter integers
separated by commas.

Converting Input to Array of Numbers: Once the user enters the numbers, the input string is split
by commas using split(","), which separates individual numbers. Then map(Number) is used to
convert each string representation of a number into an actual number and store them in an
array named num.

3d.)
i.) Sequencing refers to the order in which individual statements or actions are executed within a
program. In this code, sequencing is evident in the order of statements within functions and the
flow of execution.

As shown below:
let input = prompt("Enter numbers separated by commas:");
let num = input.split(",").map(Number);
// Convert input string to array of numbers
let average = calculateAverage(num);
alert("The average of the numbers is: " + average.toFixed(2));
// Display average with two decimal places
if (average >= 0) {
alert("This is a positive number");
}
else {
alert("This is a negative number");
}

ii.) Iteration involves repeating a set of instructions a specified number of times or until a
condition is met. In this code, iteration is implemented using a for loop to iterate over the
elements of an array.

Example:

function calculateAverage(num) {
let sum = 0;
for (let i = 0; i < num.length; i++) {
sum += num[i];
}
return sum / num.length;
}
—-------------------------------------------------------------------------------------------------------------------------------------------------------
-
In the calculateAverage function, the for loop iterates over the elements of the num array to
calculate the sum of all numbers.

iii.)Selection involves making decisions based on conditions. In this code, selection is


implemented using an if-else statement to determine whether the calculated average is positive
or negative.

Example:
if (average >= 0) {
alert("This is a positive number");
}
else {
alert("This is a negative number");
}
—-----------------------------------------------------------------------------------------------------------------------------------------------------
Here, depending on whether the average is greater than or equal to zero, the program selects
and executes either the block of code within the if statement or the block within the else
statement.

3e.) The final output of this code is the average of the numbers we have given and a printed
statement saying whether the average is “positive” or “negative”.

You might also like