I have the code (JavaScript + a bit of Html) below taken from Data Visualization with Python and JavaScript b Kyran Dale (I am a total novice where the web is concerned - a long time spent as a DBA not concerned with matters internet), but am now getting into a Masters as a mature student and I need to knuckle down and actually learn this stuff!! :-) )
JavaScript (do_student_data.js):
var studentData = [
{name: 'Bob', id:0, 'scores':[68, 75, 76, 81]},
{name: 'Bob', id:0, 'scores':[68, 75, 76, 81]},
{name: 'Bob', id:0, 'scores':[68, 75, 76, 81]},
{name: 'Bob', id:0, 'scores':[68, 75, 76, 81]},
];
function processStudentData(data, passThreshold, meritThreshold){
passThreshold = typeof passThreshold !== 'undefined'? passThreshold: 60;
meritThreshold = typeof meritThreshold !== 'undefined'? meritThreshold: 75;
data.forEach(function(sdata){
var av = sdata.scores.reduce(function(prev, current){
return prev + current;
},0)/sdata.scores.length;
sdata.average = av;
if(av > meritThreshold){
sdata.assessment = 'Passed with merit';
}
if(av > passThreshold){
sdata.assessment = 'Passed';
}
else{
sdata.assessment = 'Failed';
}
console.log(sdata.name + "'s (:id " + sdata.id + ") final assessment is: " + sdata.assessment.toUpperCase());
});
}
Html:
<!-- index.html -->
<!DOCTYPE html>
<meta charset = "utf-8">
<head>
<!-- this head section has to be added otherwise a file not found :8000/favicon.ico:1 error occurs
see here https://stackoverflow.com/questions/31075893/im-getting-favicon-ico-error
-->
<link rel="shortcut icon" href="#">
</head>
<div id = 'viz'></div>
<script type = "text/javascript" src = "./do_student_data.js"></script>
<script>processStudentData(studentData)</script>
I run
python3 -m http.server
from the directory where the files are placed and then open up Chrome (recommended by Dale) and hit Ctrl-Shift-J to get the console but there' nothing there - it's empty.
I've tried putting
<div id = 'viz'>processStudentData()</div>
with and witout parentheses but still no joy!
I've also tried doing the following to no avail:
<div id = 'viz'></div> <!-- dummy div -->
<script>processStudentData(sdata)</script> <<---- ADDED LINE!!!
<script type = "text/javascript" src = "do_student_data.js" async></script>
I have processStudentData(XXXX) both with and without studentData in place of XXXX (i.e. otherwise blank!)
I would like to know
a) how to get the code to work, and more importantly
b) the calling conventions for JavaScript within the browser like this - any references, URLs &c. gratefully received, but a quick explanation would also be appreciated. Do I need to call my code within a div or other section?