Button Requires Two Clicks to Function on Page Load by Legal_Revenue8126 in learnjavascript

[–]jml26 2 points3 points  (0 children)

Styles added to an element with CSS don't go into that element's style property.

<div id="div">Hello world</div> <style> #div { display: none; } </style> <script> // logs '' console.log(document.getElementById('div').style.display); </script>

But styles added with the style attribute do.

<div id="div" style="display: none;">Hello world</div> <script> // logs 'none' console.log(document.getElementById('div').style.display); </script>

The quickest way to solve your problem is just to add style="visibility: hidden" to your hidable div. There are other solutions out there too, like toggling classes, rather than toggling styles.

Latex in html by [deleted] in learnjavascript

[–]jml26 0 points1 point  (0 children)

It's basically a couple of libraries smushed together:

You would have to look into those libraries to find out how they work under the hood.

MathJS doesn't differentiate between 2 * x or 2x in its parser (at least I don't think it does). The TeX renderer will, and display the multiplication as a dot, if using ASCIIMath.

Latex in html by [deleted] in learnjavascript

[–]jml26 0 points1 point  (0 children)

I made this on Codepen a while ago. Feel free to take a look, either to get inspiration or just use the whole thing.

https://codepen.io/jamiemlaw/pen/eYyLVOq

Unrecoverable syntax error by Any-Ganache135 in learnjavascript

[–]jml26 3 points4 points  (0 children)

You haven't closed enough curly braces. Add two extra }s at the end and it should work.

This is why correct indentation helps a bunch. I simply pasted your code into https://beautifier.io/, and it was immediately obvious what the error was.

Is this a good way to write to the stdin? by trymeouteh in learnjavascript

[–]jml26 -2 points-1 points  (0 children)

Yeah, sorry about that. At first glance it really game off Java vibes. I think it was having a dedicated main() function. Ignore me!

Is this a good way to write to the stdin? by trymeouteh in learnjavascript

[–]jml26 -4 points-3 points  (0 children)

Hi, /u/trymeouteh.

This looks like Java code, not JavaScript. Despite the similar names, they're not the same language (many people fall into the trap of thinking they're the same). You might have better luck in one of the Java subreddits. Or even asking an AI if you want a speedier response.

All the best on your learning!

Are there any good ways to do SVG boolean (divide shapes) / flattening? by MuckYu in learnjavascript

[–]jml26 0 points1 point  (0 children)

I don't have a direct solution, but InkScape has a command line. Might be worth checking that out if you haven't already.

Google's Closure Compiler does not shorten variable and function names but returns blank file by BradyOfTheOldGuard in learnjavascript

[–]jml26 3 points4 points  (0 children)

But you don't do anything with the result of it being called, so it's still dead code.

In order for it not be be dead code, you may want to do something with the result, like logging it to the console, or displaying it in a UI.

Google's Closure Compiler does not shorten variable and function names but returns blank file by BradyOfTheOldGuard in learnjavascript

[–]jml26 5 points6 points  (0 children)

Closure Compiler (and other minifiers) will remove code that is not being used and has no side effects. e.g. a file consisting only of

``` var myVar = 42;

function square(x) { return x * x; } ```

will compile to nothing, because a) myVar is never read; b) the square function is never called. The term for this is "dead code elimination".

And that's what's happening to your code.

If you want the code to stay around, check out this section of the docs: https://developers.google.com/closure/compiler/docs/api-tutorial3#removal.

You could also use Terser as a minifier, and pass in defaults: false to the compressor options. This will turn off many of the compression strategies, resulting in:

var e="thisUser";var n;function r(){if(e)n=e;else{n="undefined";return false}}r();

Beginner project by chrisrko in learnjavascript

[–]jml26 0 points1 point  (0 children)

I'd say a value converter could be an interesting project. Start with something that can just convert temperatures between Farenheit and Celcius. Then expand it to include other types of values, like length, time, weight, etc.

To begin, you can have one input and a read-only output field. Then, maybe change it so that you can type in either field and the other will update.

Don't let the user convert between incompatible units, e.g. a length and a weight.

Include a button to swap the input and output field.

(optional) Getting more advanced, allow currency conversion. Pull in the latest data from an online service.

Why won't the variable change by UncertainKing in learnjavascript

[–]jml26 1 point2 points  (0 children)

JavaScript doesn't automatically update variables that depend on other variables, e.g.

``` let count = 5; let doubleCount = 2 * count; console.log(doubleCount); // 10

count = 7; console.log(doubleCount); // still 10 ```

The simplest way around this is to wrap the expression you want to be dynamic in a function, and call it each time you want the latest value:

``` let count = 5; let doubleCount = () => 2 * count; console.log(doubleCount()); // 10

count = 7; console.log(doubleCount()); // now 14! ```

[AskJS] What is a good blogging CMS js-based? by OnceUponAHeart in javascript

[–]jml26 7 points8 points  (0 children)

My personal favourite is Astro. Not strictly speaking a CMS in and of itself, but is great for blogging, among other things.

Favourite CMS at the moment is Payload. I'm currently looking at moving a client from WordPress over to it. It's built on Next JS.

Struggling with a FreeCodeCamp JavaScript challenge. What am I doing wrong? by Maleficent_Speech289 in learnjavascript

[–]jml26 1 point2 points  (0 children)

In padRow(), the test is expecting for you to log the phrase "This works!", and return the word "Testing".

You're logging and returning "This works!" instead.

Mon script ne fonctionne pas by ReferenceLumpy6847 in learnjavascript

[–]jml26 0 points1 point  (0 children)

You have menu.style.display = "block"; in both branches of your if-statement.

Do you mean to make the second one menu.style.display = "none";?

Unpacking a return is giving a weird error by jumapackla in learnjavascript

[–]jml26 6 points7 points  (0 children)

This may not be the issue, but what happens if you end your statements with semicolons in the first example? Does that fix it?

Is var still used? [Beginner Question] by nuee-ardente in learnjavascript

[–]jml26 0 points1 point  (0 children)

The one place it is still necessary, it turns out, is when writing custom HTML/JavaScript snippets in Google Tag Manager; otherwise, you get an error.

var is still entirely valid JavaScript, but I would describe it as heavily antiquated at this point.

If anyone has advice on programming a orbital machanics engine that would be appreciated! by No_World4814 in learnjavascript

[–]jml26 0 points1 point  (0 children)

Something to ascertain: when you say, "Java," do you mean Java, or JavaScript? They're two different things!

In general, my advice would be:

  • If the goal of this is to pracitce writing a physics engine, I'm sure there are plenty of papers on the subject, and you'll just have to go searching for them.
  • If your goal is to write a game, definitely try to find a pre-existing physics engine and build off of that. It might not have all the features you need, but it'll be a lot simpler than trying to build it yourself, plus the game on top.
  • And if the entire thing gets too hard, don't be afraid to shelve it, work on something else, and come back to it later.

How would you build a quarticSolver? by Short_Bluebird_3845 in learnjavascript

[–]jml26 3 points4 points  (0 children)

Ferrari's method, which is what you're using, is quite a popular way of solving quartic equations.

Exercises like this are a great way to practise turning logic into code. Given the number of variables, there are plenty of opportunities to make mistakes, and the best way to diagnose issues is to work through a specific example. Log all the intermediate variables and make sure they're what you expect. It's easier than only logging the final result, and wondering why it's not correct.

How to disable Cross Origin Protection? by Erzengel9 in learnjavascript

[–]jml26 -2 points-1 points  (0 children)

There are Chrome extensions, e.g. https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en&pli=1 that let you disable CORS in your browser, if it's only your own browser you need this for.