The Student Room Group

Help in code

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. :smile:

My code so far:
//Test with 7 students

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

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

if(studentAge > 16)
{
document.writeln("<br>"+studentAge+" Error!":wink:;
}
if(studentAge < 13)
{
document.writeln("<br>"+studentAge + " Error!":wink:;
}
if((studentAge >= 13) || (studentAge <=16))
{
document.writeln("<br>"+studentAge + " Accepted Age!":wink:;
}
}

//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
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)
Reply 2
You almost mentioned a good solution, use an array of objects like this to store the initial results:


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


const getScoresForHouse = houseName => {
return reactionTimeRecords.filter(record => record.house === houseName)
}

Quick Reply