My JavaScript stopped working

Description
I had some JavaScript to calculate Median and it was working for the last 8 months, today it stopped and greatly impacted my app.

How to replicate
View the JavaScript which no longer works:

const array = p1.split(',').map(Number);
const mathjs = await import("https://cdn.skypack.dev/mathjs");
return mathjs.median(array);

I had to fix quickly to get the app working again, I added a new column with the following code and this new JavaScript works:

const array = p1.split(',').map(Number);

// Updated URL for mathjs
const mathjsUrl = "https://cdn.jsdelivr.net/npm/mathjs@10.5.1/lib/browser/math.js";

// Load and execute the mathjs script
const response = await fetch(mathjsUrl);
const scriptText = await response.text();
eval(scriptText);

// Access the mathjs global object
const mathjs = math;

// Calculate the median
return mathjs.median(array);

Why did the old one stop working?

Seems like Skypack altered something in how that package is served.

This works:

const array = p1.split(',').map(Number);
const mathjs = await import("https://cdn.skypack.dev/pin/mathjs@v13.0.0-rp8kkxRaiE5hwGJ9ExQK/mode=imports/optimized/mathjs.js");
return mathjs.median(array);

It’s this URL when you access https://cdn.skypack.dev/mathjs.

1 Like

Thank you very much for your help on that. I have used the code you provided it’s working great. One thing to point out is that it was very hard to debug the issue, I first thought there was an error with the template column. The reason for this was that the JS column in my glide computed column was showing the values for about an hour into breaking. So all other columns in my app using the JS stopped reading any values, but the JS itself still showed the values, after about an hour the JS finally rendered as blank and I was able to find the source of the problem.

1 Like

Ya, there are times where you import packages and the original site has problems with new updates. Just let us know if you need help!

Given JS is an experimental feature, I would strongly suggest to use only inline short vanilla JS code and avoid any external script.

Otherwise, you can end up with much worse errors. Luckily you found solution fast, but I doubt it will always be the case.

Not to mention, you have no idea of the response time while loading an external script.

I simply have a need to calculate the median. Glide does not offer this in any computed column and I know no other way to get this figure other than Js. Median is part of my ‘Market Value’ equation and thus very critical. Can you suggest a better way? Thank you.

I’m not experienced with Js.

Working on it, will do tomorrow.

Thank you very very much.

Here’s vanilla JS.

const array = p1.split(',').map(Number);

array.sort((a, b) => a - b);

const mid = Math.floor(array.length / 2);

if (array.length % 2 === 0) {
    return (array[mid - 1] + array[mid]) / 2;
} else {
    return array[mid];
}
1 Like

@Cameraville @ThinhDinh Looks like your median JS function, thank you @ThinhDinh. p1, p2 etc are input columns for the JS code.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.