r/JavaScriptHelp • • Jan 20 '22

❔ Unanswered ❔ Are there any good libraries for skeleton loading screens for vanilla js?

2 Upvotes

I want to create a skeleton for a horizontal bar chart, but I'm having trouble finding a good library for it. Most of them are either for React or Vue and my page is in vanilla js.

The closest thing I could find was css-skeletons, which lacks support for horizontal bar charts (only vertical bar charts are considered).


r/JavaScriptHelp • • Feb 12 '22

✔️ answered ✔️ Noob needs help with a button

2 Upvotes

I'm working with html, css and javascript and I can't figure out how to make a button do what I want it to.

<button id=btn class="button" onclick="rmv()"> Click here!</button>

<script>

function rmv() {

var element = document.getElementById("heading");

element.remove ();

var element = document.getElementById("paragraph");

element.remove();

var element = document.getElementById("btn");

element.remove(); }

</script>

I've managed to make the button 'disappear' itself and the text that was on the page before it was clicked, but I also want to it to make the page change colors and new text to appear, but I don't know how. I succeeded in making the colors change and the text appear but on a different page:

<style>

@keyframes background-color {

0% {background-color: #f3bcc0}

20% {background-color: #acf3cc}

40% {background-color: #f5eb18}

60% {background-color: #cbe832}

80% {background-color: #ff9317}

100% {background-color: #c7658f}

}

body{

animation: background-color 5s infinite;

animation-direction: alternate;

}

h1 {text-align: center;}

</style>

<h1> Text </h1>

Could someone write the code for the button to do this? I would really appreciate the help, I know it's probably a silly question but i just started learning a few days ago :)


r/JavaScriptHelp • • Jan 31 '22

❔ Unanswered ❔ Help with external JSON file to JavaScript array

1 Upvotes

Hi guys!

First time on r/JavaScriptHelp

working on extending my javascript skills past the occasional jQuery DOM manipulation, and I've run up against something I don't quite get.

I'm trying to load an array of arrays from an external JSON file. The external file (items.json) looks like:

 [
    [
"./listimages/steamtrain.jpg",
"1804",
"Steam Trains"
    ],
    [
"./listimages/beiber.jpg",
"2009",
"Justin Beiber 'One Time'"
    ],
    [
"./listimages/avril.jpg",
"2002",
"Avril Lavigne 'Complicated'"
    ],
    [
"./listimages/kfc.jpg",
"1952",
"Kentucky Fried Chicken"
    ],
    [
"./listimages/coke.jpg",
"1892",
"Coca Cola"
    ]
]

my code to pull this to an array is:

$.getJSON('items.json', function (data) {
if (data.length > 0) {

$.each(data, function (index, value) {
items.push(value);       // PUSH THE VALUES INSIDE THE ARRAY.
            });
        } else {
console.log("no data");
        }
console.log(items);
    });

The data does get assigned somehow because the console shows an array of 5 arrays with 3 items each, and expanding shows the items. However, the items are not accessible via index like items[0][0]. Get error cannot read properties of undefined.

Incidentally I can do a direct assignment and get the same result

items = data;

Please help me understand what I am not understanding about how this translates, and how to get a direct array translation.

Thanks much.


r/JavaScriptHelp • • Jan 22 '22

✔️ answered ✔️ I am trying to create an error text if there is no name in the input. Can someone explain why the code does not work?

1 Upvotes

Beginner here.... can someone explain why the code does not work? (suggesting a working solution is also fine)

https://codepen.io/tuggypetu/pen/QWqeyKY

been trying since yesterday...

=> I am trying to create an error text if there is no name in the input (edited)


r/JavaScriptHelp • • Feb 01 '22

❔ Unanswered ❔ JS Function and return

1 Upvotes

https://imgur.com/gallery/NsRe1Bi

In this code, the console log for width and height have been printed as per the console.log that is placed inside the function(). But for dimensions=[width, height], it logs [undefined,undefined]. Now I understood the part in tutorial that it require a return statement to display the value.

But what I am confused is if in above case we do get console log of string for both width and height. Then why the same string are not being displayed in dimensions=[width,height] array.

Is it because inside function we are only doing a console.log ( print statement ) rather than storing the value anywhere?

Source: https://www.youtube.com/watch?v=2Ji-clqUYnA&t=776s&ab_channel=CodingAddict


r/JavaScriptHelp • • Feb 07 '22

❔ Unanswered ❔ SyntaxError! please help....

2 Upvotes

I am doing a Rock, Paper, or Scissors project

code:

const getUserChoice=userInput =>
{
if(userInput==='rock'){
return userInput;
}else if(userInput==='paper'){
return userInput;
}else if(userInput==='scissors'){
return userInput;
}else{
return 'error';
}
}
function getComputerChoice(){
const randomnumber=Math.floor(Math.random()*3);
switch(randomnumber){
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice,computerChoice){
if(userChoice===computerChoice){
return 'the game was a tie';
} if(userChoice==='rock'){
if(computerChoice==='paper'){
return 'computer won!';
}else{
return'you won!'
}
}else if(userChoice==='paper'){
if(computerChoice==="scissors"){
return 'computer won!';
}else{
return "you won!";
}
}else if(userChoice==='scissors'){
if(computerChoice==='paper'){
return 'computer won!';
}else{
return 'you won!';
}
}
}
function playGame(userChoice,computerChoice){
let userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice,computerChoice));
}
playGame();

//when I use this code, it keeps showing /home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:54 let userChoice = getUserChoice('scissors'); ^ SyntaxError: Identifier 'userChoice' has already been declared

//but if I replace the last function code with the following:

const playGame = () => { let userChoice = getUserChoice('scissors');

const computerChoice = getComputerChoice(); console.log('You threw: ' + userChoice); console.log('The computer threw: ' + computerChoice); console.log(determineWinner(userChoice,computerChoice)); } playGame();

it works, please help, I don't know the reason.


r/JavaScriptHelp • • Oct 28 '20

❔ Unanswered ❔ Implementing stopwatch function into JS game

2 Upvotes

Hi. I need help adding a stopwatch to a JS game. I want the stopwatch to start when the player starts a round and then pause when the round is over. And when the rounds over, I want the paused time to be displayed when the player has the option to either play the next round or retry the same round. Whenever the round starts the stopwatch time resets. The link to the game’s code is here->: Factoring Game . I have a stopwatch code if needed.


r/JavaScriptHelp • • Oct 18 '20

❔ Unanswered ❔ SyntaxError: Missing catch or finally after try...while compiling ejs

4 Upvotes

Could you please take a look at this stackoverflow question about

SyntaxError: Missing catch or finally after try ... when compiling ejs
I don't have any try statements in my files. My syntax is correct but I don't know the cause of the error. I have also checked the ejs docs. Maybe my computer is not compiling ejs correctly? Do you have any hints?


r/JavaScriptHelp • • Oct 21 '20

❔ Unanswered ❔ Is there a way to read an MD file and translate it to HTML without using a server?

2 Upvotes

I'm trying to edit files in Markdown the same way I do HTML files.

With HTML, I edit the file in a text editor (I use Sublime Text) while keeping a browser window open to file:///Users/JesusIsMyZoloft/Desktop/etc. Whenever I want to see the changes, I simply save the file and refresh the page. This works well with HTML, but I'd like to extend it to MD files as well. (And eventually LaTeX, but that's another story)

The idea is to load an HTML document with nothing in the body, but a <script> tag in the head. This would call a script that would read the file, translate it into HTML, and then use jQuery to populate the body with the new content.

So there are two problems: reading the file and translating the file. One workaround I've tried for reading the file was to add a prefix and suffix like so:

var md = `

# Heading
Content of the file.

`

...and then add the following tag to the head of the HTML page:

<script type="text/javascript" src="file.md"></script>

...calling the MD file, as a JS file. This works... kinda... but I'm wondering if there's a more elegant solution. (Preferably one where Sublime doesn't try to warn me about the syntax error with big pink bars.)

The second problem is translating the file. I've found several repositories on GitHub for converting MD to HTML, but they all seem geared toward a Node server. What I need is a single function that takes in the MD file contents as a String, and returns it as HTML. (I suppose if it took in the file path as a string, and returned the HTML that would work too, solving both problems at once.)

Any help is appreciated!


r/JavaScriptHelp • • Oct 21 '20

❔ Unanswered ❔ What is this code doing exactly?

1 Upvotes

Hello all, so this code is actually in TypeScript, but I’m having trouble understanding exactly what its doing. I understand that the output works nicely and that you can drag and drop elements by the mouse in the list. That's all what it looks like it’s doing. However maybe it’s because I mainly code in Python and haven’t touched JavaScript in a long while that I’m not fully understanding this like I should. Any help/advice is greatly appreciated!

https://imgur.com/a/51Rn3FW

Here’s all the code in JSFiddle so that it’s easier to see: https://jsfiddle.net/AriannaC/x71f0srn/2/ However I get an error when running it here when I shouldn’t


r/JavaScriptHelp • • Nov 06 '20

❔ Unanswered ❔ I want to code my table top game

1 Upvotes

I’ve learned a lot of the basics for JavaScript but still don’t know really where to begin. I want to practice things that will be relevant to my goal: a turn based strategy game.

I already have the game designed as a dice and paper game, but now that I know how variables, objects, functions etc... work, what do I do?


r/JavaScriptHelp • • Oct 06 '20

⚠️ Moderator ⚠️ New mod!

4 Upvotes

G'day everyone, I submitted a reddit request a while back for this sub as your previous mod seemed to abandon the sub. I'm going to start tidying things up shortly and then I'll unrestrict he sub. All the previous spam posts will be removed, it seems a bit petty to ban them though so the users who posted them can continue posting.

In the meantime, do any of you guys have any suggestions? All suggestions will be welcomed!

Thanks, Fastercoder4000

Update 1: all messages in response to modmails have been sent.


r/JavaScriptHelp • • Oct 16 '20

❔ Unanswered ❔ Is it possible to check whether a website is down every so often and update a discord embed with the current status of the site?

2 Upvotes

Hey, Is it possible to check whether a website is down every so often and update a discord embed with the current status of the site? If it is would someone be able to give me some sample code? I really don't know where to start with this one. I know how to send Discord embeds I just don't know how to update a single one every let's say 60 seconds with the current status of a website.

Thanks in advance!


r/JavaScriptHelp • • Oct 18 '20

⚠️ Moderator ⚠️ Post flair clarification

1 Upvotes

Hey all, just to quickly clarify something the advice flair is when you are offering advice to people and unanswered is when you're asking something.

Please remember to set the flair as answered when you get answers.


r/JavaScriptHelp • • Oct 30 '20

❔ Unanswered ❔ [Help] OAuth 2.0 + MTLS from FaaS

2 Upvotes

Hi, is there a way to make an api request using MTLS and OAuth 2.0 from a FaaS? Something like a JavaScript library that could help me?


r/JavaScriptHelp • • Oct 25 '20

❔ Unanswered ❔ This is probably easier than it seems to me.

2 Upvotes

I want to figure out how I can tell the system to stop when the Unassigned item hits a certain number. For example, if I start the system and I have a target of 5 unassigned items it will run until the unassigned items hits 5.

https://jsfiddle.net/b95fe18k/2/


r/JavaScriptHelp • • Nov 05 '20

❔ Unanswered ❔ promises help

2 Upvotes

so still trying to learn javascript and im struggling when it comes to working with promises. like this code for example.

myFunction().then(value => {
    console.log(value); // ← need to move this
});

console.log(value); // ← to here

async function myFunction() {
    return 'this is my function';
}

so my i want to move the value of the thenable down outside the promise how would i do that? one way that i tried was like this.

let newVal;

myFunction().then(value => {
    newVal = value;
});

console.log(newVal);

async function myFunction() {
    return 'this is my function';
}

but the variable newVal still comes up undefined. how can i work around this to use that thenable outside the method?


r/JavaScriptHelp • • Nov 08 '20

❔ Unanswered ❔ I need help combining Math.random()

1 Upvotes
package lotterynumbergenerator;

/**
 *
 * @author ismai
 */
public class NumberGenerator {
    
    public int winningNumbers(int min, int max) {
        double rand = Math.random();
        double rand1 = Math.random();
        double rand2 = Math.random();
        double rand3 = Math.random();
        double rand4 = Math.random();
        double rand5 = Math.random();
    
        
        int randomNum = (int) (rand * (max - min + 1)) + min;
        int randomNum1 = (int) (rand1 * (max - min + 1)) + min;
        int randomNum2 = (int) (rand2 * (max - min + 1)) + min;
        int randomNum3 = (int) (rand3 * (max - min + 1)) + min;
        int randomNum4 = (int) (rand4 * (max - min + 1)) + min;
        int randomNum5 = (int) (rand5 * (max - min + 1)) + min;
    
return randomNum;
    }

In an instant Instead of only returning the one randomNum, I want to return all 6 random's. Can someone help me do this!


r/JavaScriptHelp • • Oct 15 '20

✔️ answered ✔️ Comp Sci - Assignment

3 Upvotes

Hi, so I have a comp sci assignment which requires me to do so:

Create a small program that demonstrates your understanding of the materials researched. Your program must have, at least, the following:

  • Objects

  • At least one of:

    • Abstract classes

    • Interfaces

    • Inheritance

  • Files, by including:

    • Reading and writing of a text file, or

    • Reading and writing of a binary file, or

    • Reading from an API and writing to a text file, or

    • Reading/writing JSON information, or

    • Reading from and writing to a database (e.g. SQL, MongoDB, etc.)

  • Conversion of datatypes (int to string
    , string to int
    , float to int
    , etc.)

    • This is most easily completed when you are reading/writing files

I decided to make my program themed about space - so a class about planets all that. I've started working on the program but I'm confused about the "conversion of datatypes." How does that work? I'm completely new to Javascript and therefore I would appreciate any help!


r/JavaScriptHelp • • Oct 28 '20

❔ Unanswered ❔ Script to scroll down to bottom of Facebook page?

3 Upvotes

Hi friends, I need to scroll down to the bottom of the page without doing it manually which takes forever. (profile, not the feed)

I used this script - https://gist.github.com/eykanal/a0b30e035d8c15995deeffec6ab21866

The only problem is that when I do that and I want to scroll back up to review the posts it keeps bouncing back down. Does that mean the code is still running? how do I stop it once it get done?

any and all other solutions or other/better scripts will be **highly** appreciated

I am a beginner (literally looked into this just yesterday) so hope this was informative enough


r/JavaScriptHelp • • Oct 22 '20

❔ Unanswered ❔ Help writing working backend Code

1 Upvotes

Hi, I'd say I'm an intermediate level coder at best. To be more specific, I'd like to know if anyone is interested in helping me with my code via Zoom calls where I can screen share problem code. I'm not always the best at explaining my issues and having someone to actively talk through my problems with would tremendously help me.

A little background info is that I'm writing a program in a swift environment using Firebase for my backend and database. I'm having difficulty writing my backend functions in Javascript and debugging them. Ideally, some background with swift would probably benefit but I can provide much more details to anyone interested in helping me with my learning curve.


r/JavaScriptHelp • • Oct 15 '20

⚠️ Moderator ⚠️ Total reopening of sub!

3 Upvotes

For real now! Do any of you guys have any suggestions? You'll notice that we have some shiny new flairs and new rules! Please check them all out. Enjoy the sub :)


r/JavaScriptHelp • • Dec 09 '17

Little help please, how to assign default action to right click?

1 Upvotes

How to assign right click in this case to do it's default action? Now it do what the middle button does.

document.getElementById("googlelink").addEventListener("click", redirectToYahoo);
document.getElementById("googlelink").addEventListener("auxclick", redirectToYahooNewTab);
document.getElementById("googlelink").addEventListener("oncontextmenu", rightClick);

function redirectToYahoo() {
 	window.location.href = "https://www.yahoo.com";
}

function redirectToYahooNewTab() {
 	window.open('https://www.yahoo.com', '_blank');
}

function rightClick() {};

r/JavaScriptHelp • • Dec 12 '17

I need help. Word Generator script

1 Upvotes

Im trying to make a word generator that pulls up three random words at a time everytime the user clicks the button. Im a beginner and have followed afew tutorials and this is what I have so far.

<h1>Idea Generator</h1>

<FORM NAME="WordForm"> <INPUT TYPE=TEXT NAME="WordBox" id="wordbox"><BR> </div> <INPUT TYPE=BUTTON VALUE="Generate" <button onClick="PickRandomWord(document.WordForm)" id="button"> </div> </FORM>

var NumberOfWords = 28

var words = new BuildArray(NumberOfWords)

words[1] = "Rose" words[2] = "Lily" words[3] = "Daffodil" words[4] = "Daisy" words[5] = "Tulip" words[6] = "Sunflower" words[7] = "Dandelion" words[8] = "Lavender" words[9] = "Hydrangeas" words[10] = "Ranunculus" words[11] = "Fern" words[12] = "Aloe Vera" words[13] = "Marigolds" words[14] = "Narcissus" words[15] = "Chrysanthemums" words[16] = "Carnations" words[17] = "Alstromeria" words[18] = "Cherry Blossom" words[19] = "Freesia" words[20] = "Iris" words[21] = "Orchids" words[22] = "Violets" words[23] = "Ivy" words[24] = "Mouse" words[25] = "Rat" words[26] = "Vole" words[27] = "Mole" words[28] = "Rabbit"

function BuildArray(size){ this.length = size for (var i = 1; i <= size; i++){ this[i] = null} return this }

function PickRandomWord(frm) {

var rnd = Math.ceil(Math.random() * NumberOfWords)

frm.WordBox.value = words[rnd] }


r/JavaScriptHelp • • Dec 09 '17

Passing multiple lines of code as a parameter

1 Upvotes

Hello guys, I've got a curious question here.

I am presenting a security project that can provide alternatives to a JS injection based attack. I am doing this by having a shopping cart that accepts a coupon code, which is the point of entry. I have code which looks like this:

jQuery(document).ready(function() {
    $('#submitcode').click(function(e) {
        var code = $('input[name=couponcode').val();
        updateCode(code);
    });
 })

'#submitcode' is the button that the user clicks and their coupon code is in the input field which is grabbed by jQuery and passed to the updateCode function which looks like this:

function updateCode(c) {
    code = c; //code is a global variable
    updateSubtotal();
}

From here, the shopping cart updates the subtotal, tax, shipping, and applies the coupon code and deducts the price before showing the final price.

Everything up to this point is working as intended. Now, I attempt to inject the following code:

newCouponCode;acceptableCouponCodes.push(newCouponCode);discounts.push(1);

acceptableCouponCodes is an array which has all of the acceptable codes which can provide a discount. discounts is an array which has all of the matching discounts as percentages.

Entering this code into the text field and clicking the submit button does not do anything however. Adding a console.log(code); statement to the updateCode function and opening up the browser console shows the following as the console.log:

newCouponCode;acceptableCouponCodes.push(newCouponCode);discounts.push(1);

So, that leads me to believe that you cannot pass code like this in the way that I am doing (from a jQuery click function to another function with the input passed as a parameter) or I need to include a ending quote after newCouponCode;. Doing the latter -- so passing in newCouponCode;%22acceptableCouponCodes.push(newCouponCode);discounts.push(1); -- causes the entire string to be in the console.log statement and the discount is not applied.

Sorry for the long post everyone. I tried and make this as short and concise as possible.