Help in code Watch

Dabluemini
Badges: 7
Rep:
?
#1
Report Thread starter 7 months ago
#1
Hi all I've been practicing my javascript skills with igcse computer science paper 2 (cie) case studies , since the questions are nice . I've been noticing that there is a recurring of questions such as:

TASK 2 – Output school house based statistics
Using your sample, calculate and output the average reaction times for students in Saturn and students in Mars. (CIE computer science paper 2 February/March 2017 task 2).
It basically asks from the first task in which you made one dimensional arrays for the Age, house and react time , find the average reaction times for each one of the houses ( Saturn and Mars) .

I'm unsure how to separate the reaction times into the houses , I was thinking of using multidimensional arrays or objects , or overwriting a new array for each one of the houses . I'm unsure.
It's best if you can write a solution in psuedo-code. Thank you for you help in advance.

My code so far:
//Test with 7 students

//Student age section ------------------------------------------------------------------------------------
var studentAge = ["Num","Num","Num","Num","Num","N um","Num",]; // represents the age of the student

for(var i = 0 ; i < studentAge.length ; i ++)
{
studentAge[i] = parseInt(prompt("please enter a number");

if(studentAge[i] > 16)
{
document.writeln("<br>"+studentA ge[i]+" Error!";
}
if(studentAge[i] < 13)
{
document.writeln("<br>"+studentA ge[i] + " Error!";
}
if((studentAge[i] >= 13) || (studentAge[i] <=16))
{
document.writeln("<br>"+studentA ge[i] + " Accepted Age!";
}
}

//Student House section ------------------------------------------------------------------------------------
var studentHouse = ["Saturn","Mars","Saturn","Mars", "Saturn","Mars","Saturn"]; //represents student house


//Student House section ------------------------------------------------------------------------------------
var studentReactTime = [13.0,14.5,16.2,15.1,15.2,16.2,17 .8]; //floats are used since we are using time
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 7 months ago
#2
I'd suggest avoiding multi-dimensional arrays, they usually add confusion to your code and rarely have any advantage over a single-dimensional array using plain objects and filtering. While there are never any 'right' or 'wrong' ways of structuring data, it's better to look for ways of making sure the code conveys your intent and that its purpose is clear and self-evident for someone reading it.

There are two main problems with using multi-dimensional arrays to group data - Firstly, you'd end up with a lot of "magic" numbers everywhere, and secondly that you'd end up with a much weaker grouping of your data - it'd be easy for your arrays to end up falling out-of-step with each other, so you'd end up writing extra unnecessary code to prevent that from happening

Of course, the most important thing is to choose something which works. Since you're dealing with data which is logically related, consider grouping those data items together into their own object -- you can have an array of objects which each only contain data - very similar to a 'record' in a table or database. For example:
https://repl.it/repls/MutedFluffyClimate

There are various neat ways you can use arrays to only select the data that you need -- for example, the filter() function can be useful if you want to be able to do something with a sub-set of an array which matches some condition:
https://www.w3schools.com/jsref/jsref_filter.asp
(Actually it's much easier to perform operations like filter with a one-dimensional array - so that's another reason to avoid multi-dimensional arrays)
1
reply
amrit_sv
Badges: 7
Rep:
?
#3
Report 7 months ago
#3
You almost mentioned a good solution, use an array of objects like this to store the initial results:
Code:
let reactionTimeRecords = [{age: 15, house: "saturn", reactionTime: 10},{age: 14, house: "saturn", reactionTime: 11},{age: 14, house: "saturn", reactionTime: 8},{age: 15, house: "saturn", reactionTime: 8},{age: 14, house: "mars", reactionTime: 10},{age: 15, house: "mars", reactionTime: 9},{age: 14, house: "mars", reactionTime: 7},{age: 15, house: "mars", reactionTime: 10}]
then you can write a simple function to return the records based on what you passed in
Code:
const getScoresForHouse = houseName => { 
    return reactionTimeRecords.filter(record => record.house === houseName) 
}
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Warwick
    Undergraduate Open Day Undergraduate
    Fri, 21 Jun '19
  • University of Bath
    Find out about life at the University and discover our diverse range of Undergraduate courses. Our course areas include the Sciences, Humanities & Social Sciences, Engineering & Design, and Management. Undergraduate
    Fri, 21 Jun '19
  • University of Liverpool
    Undergraduate Open Day Undergraduate
    Fri, 21 Jun '19

How did your AQA A-level Chemistry Paper 3 go?

Loved the paper - Feeling positive (297)
32%
The paper was reasonable (439)
47.31%
Not feeling great about that exam... (114)
12.28%
It was TERRIBLE (78)
8.41%

Watched Threads

View All