apopt.comCBE 493R Data-Driven Athletics

Course / Build Lab / JavaScript for Python people

JavaScript for people who just learned Python

Same ideas, different punctuation. Python is for analysis in this course; JavaScript runs your app in the browser.

IdeaPythonJavaScript
A list of numbers
hr = [142, 150, 158]
const hr = [142, 150, 158];
Length
len(hr)
hr.length
Average
sum(hr) / len(hr)
hr.reduce((a, b) => a + b, 0) / hr.length
Loop
for x in hr:
    print(x)
for (const x of hr) {
  console.log(x);
}
Function
def pace(sec, km):
    return sec / 60 / km
function pace(sec, km) {
  return sec / 60 / km;
}
Dictionary / object
run = {'km': 10, 'min': 48}
const run = { km: 10, min: 48 };
Read a CSV
pd.read_csv('runs.csv')
await (await fetch('runs.csv')).text()  // then split lines

Three traps: JavaScript indexes from 0 like Python but has no negative indexes (hr.at(-1) instead of hr[-1]); == converts types, so use ===; and reading a file is asynchronous, so you await it.