Jump to content

Javascript Help

So I have a quiz assignment for my computer science class, which asks me to: 

 

Quote

Create a webpage that asks for a student’s name and four quiz scores. All pieces of data should be recorded in an array.  Student[0] should hold the student’s name. Student[1] should hold test score 1, Student[2] should hold student test score 2 etc. Student[5] should hold the average for that student.

 

Use prompts to collect the data. 

 

Use a function to calculate the average for the student. Output all the information using an alert.

 

Use a button to start the input.

Generally, I just dont know how to put the function in there by using Array variables. If anyone has any idea it would be mighty appreciated. 

 

I don't have much js experience, so anything slightly more advanced like loops are kind of out of the question.

 

My code so far:

 

<script>

var avg  
  
var Student = [

prompt("Enter Name","Name"), 

prompt("Enter Test Score 1","0"), 

prompt("Enter Test Score 2","0"), 

prompt("Enter Test Score 3","0"), 

prompt("Enter Test Score 4","0"), 


]


alert() //?


</script>

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

without using loops i guess you can do this:

 

var sum = parseInt(Student[1])+parseInt(Student[2])+parseInt(Student[3])+parseInt(Student[4]) //sum the 4 scores up
Student[5] = sum/4	//get the average of the 4 scores

alert("Average of scores is: " + Student[5])

I don't really see why you need a function to do this, but you can just put the same logic inside one like this:

 

 

function average(array){

return (parseInt(array[1])+parseInt(array[2])+parseInt(array[3])+parseInt(array[4]))/4  //calculate average of the 4 test scores
  
}

Student[5] = average(Student) 

alert("Average Score: " +Student[5])

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, elpiop said:

I don't really see why you need a function to do this, but you can just put the same logic inside one like this:

because a function allows you to reuse the average code for multiple students if you need to expand it.

 

I would create an array with a nest array for the results. 

 

0 = name

1 = results array

1 0 result 1

1 1 result 2

 

and so on this allows you to just sent array[1] to the function then loop it.

 

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Start</button>
<p id="results"></p>
<script>
function myFunction() {
  //empty array and nested array for results
  //this allows us to just send all the results as a single array to the average function
    var data = ['',[]];
    //set first item in array to the name
    data[0] = prompt("enter name");
    //set second item to the results as an array
    data [1][0] = prompt("test score 1");
    data [1][1] = prompt("test score 2");
    data [1][2] = prompt("test score 3");
    data [1][3] = prompt("test score 4");
    //puts average on the page. calcAv(data[1]) sends our rerults to the function below.
    document.getElementById("results").innerHTML = data[0] + "average :" + calcAv(data[1]);
}
function calcAv(test){
  var average = 0;
  //loop the array of test results
  for (var i=0; i < test.length;i++){
    average =+ test[i];
  }
  return (average/test.length);
}
</script>
</body>
</html>

Next you need to change the code a little so that you build name + results array and then append it to the main array. I'm not going to do all your homework for you ^_^ 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×