Jump to content

Javascript Functions and Arrays

Slottr

Some of you may have saw a post from yesterday asking the same thing, but now I've advanced a little in my project.

 

So this project 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.

I have limited knowledge on JS and would only like to use what I so have already - loops and related are possibly out of the question.

 

I have gotten most of it done ( I think ) but am stumped on getting one functions data into another.

 

My code: https://js.do/code/ohgoodgodwhatamidoing

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

Using your code as a base, here's a functional version: http://js.do/code/ohgoodgodwhatamidoing2. I also switched up the way you were initialising your `score` array in your `student` function, as that was a little strange.

 

You were almost there! You were parsing the arguments into the `avg` function as strings, but then referencing them as arrays. You already have the data you need, so I just through them through `parseInt` to ensure they were numbers. If the test scores could potentially be floats (numbers such as 3.14), you'll want to parse them through `parseFloat` instead, and likely round the result at the end to 2 decimal places, so to avoid any floating point number issues.

 

You could have also just past the entire `score` array into your `avg` function, and then referenced it as an array by doing `score[1] + score[2]`, etc, rather than creating 4 separate function arguments. I'd recommend reading through https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics and then http://bdcampbell.net/javascript/book/javascript_the_good_parts.pdf for a more in-depth overview.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Cherry said:

 

Ahhhh. Holy crap that makes so much sense. Thanks.

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

and what was wrong with posting in your other thread? It's the same problem no?

                     ¸„»°'´¸„»°'´ 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

×