Posts

How to extract items from a subarray and push them to the main array

I'm doing a type speed test and, for this, I'll need to get a random item from an array, split every letter, push it to a new one and re-do this several times. But, every time I split the string from the first array, returns to my new array a subarray, instead of just letters. How can I extract everything from the subarrays and put into my main array? var Pokemon = ["Bulbasaur","Ivysaur","Venusaur","Charmander","Charmeleon","Charizard","Squirtle","Wartortle","Blastoise","Caterpie","Metapod","Butterfree","Weedle","Kakuna","Beedrill","Pidgey","Pidgeotto","Pidgeot","Rattata","Raticate","Spearow","Fearow","Ekans","Arbok","Pikachu","Raichu","Sandshrew","Sandslash","Nidoran","Nidorina",&q

Trying to pull text from one field and put it into a button text Trying to pull text from one field and put it into a button text

I have a page that I can use merge fields from a CMS to dynamically adjust content; however, this functionality doesn't work with button text. Therefore, I need to use a normal text field and then take the value from that field and push it to the button text. I thought this would work, but something it's not working right. Can anyone help me figure out what I am doing wrong? <div id="ctatext" class="ctabutton">Request Pricing</div> <input type="submit" value="Get Started" data-wait="Please wait..." id="ctabutton" class="_2-0-yellow-btn-2 request w-button"> <script> $(document).ready(function () { let btntext = $('.ctabutton').text(); $('input._2-0-yellow-btn-2 request w-button').val(btntext); }); </script> from Stackoverflow

react js useState array is empty in first time click in the console log react js useState array is empty in first time click in the console log

Image
Please check my below code. I try to check the console log value, but in the first button click, the array is empty. After the second click the array will be loaded correctly. Please check this: sample The above sample can be found on Google. I console log the value as below Why is the array empty in the first click? Another question is, I need to print the value in the first click. How can I do it? Do you know any alternative method? from Stackoverflow

Javascript return undefined for an internal function Javascript return undefined for an internal function

I am doing an easy leetcode questions where we add one to a number whose digits are stored in an array. My logic works well but the returned value is undefined. Console gives the correct answer. Why? And how do I fix it? var plusOne = function(digits) { addNumber(digits.length - 1, 1, digits) function addNumber(index, value, digitsArr) { let sum = digitsArr[index] + value if(sum > 9) { let onesDigit = parseInt(sum.toString().charAt(1)) digitsArr[index] = onesDigit let twosDigit = parseInt(sum.toString().charAt(0)) addNumber(index-1, twosDigit, digitsArr) } else { digitsArr[index] = sum console.log(digitsArr) return digitsArr } } }; console.log(plusOne([1,2,9])) from Stackoverflow

Refresh page after clicking button (ok / cancel) Refresh page after clicking button (ok / cancel)

i could use some help. I want the page to refresh when someone clicks 'ok' or 'cancel' Help is appreciated, thanks alot. This is the code: Button <button class="center" type="submit" style="border: 0; background: transparent"> <img src="Playbutton.png" width="800" height="400" alt="submit" onclick="clicked();" /> </button> Javascript <script type="text/javascript"> function clicked() { if (confirm('You just clicked the button, click ok or cancel to refresh.')) { yourformelement.submit(); location.reload(); } else { return false; } } </script> from Stackoverflow

Trying to add a mp3 sound to a pressed button Trying to add a mp3 sound to a pressed button

I am as green as with Javascript but I am just wanting the page below to change the photo (which it does) update the text (which it does) and play the song of the bird once the bird name is pressed. I have it playing one song with the use of the "Play" button, then I tried to move parts of that in to each function, no joy. I have tried to an another getelementbyid (audio) but that didn't work, I am stuck when trying to have it run from within each function. I hope that is kind of clear and thanks for any help in advance. <html> <head> </head> <body> <p> <h2>Click the button to change the bird image.</h2> </p> <img id="myImg" src="http://www.outgrabe.net/bird04.jpg" width="207" height="198"> <p id="p1">Rose Robin by JJ Harrison (CC-by-SA)</p> <BR></BR> <button onclick="myFunction_1()">Pardalote<

TypeError: dispatch is not a function React and Redux TypeError: dispatch is not a function React and Redux

I get an error when I delete some lead(item). It deletes from the database but not from UI and gives this error dispatch is not a function. //lead.js from /action export const deleteLead = (id) => { return (dispatch) => { axios .delete(`/api/leads/${id}/`) .then((res) => { dispatch({ type: DELETE_LEAD, payload: id, }); }) .catch((err) => console.log(err)); }; }; //Frontend file import { getLeads, deleteLead } from "../../actions/leads"; <button onClick={deleteLead(lead.id)} className="btn btn-danger btn-sm"> Delete </button> from Stackoverflow

How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle

How do you center a rectangle in javaScript? Having the X and Y position of the rectangle being the center of the rectangle. This is a rough idea for my code. var cxt = canvas.getContext("2d"); var canvas = var canvas = document.getElementById("myCanvas"); function createSprite(x,y,width,height) { this.x=x; this.y=y; this.width=width; this.height=height; this.display(){ cxt.beginPath(); cxt.rect(this.x,this.y,this.width,this.height); cxt.fill(); cxt.stroke(); } } from Stackoverflow

Best way to make file buffer accessible outside a callback function in Javascript for large files? Best way to make file buffer accessible outside a callback function in Javascript for large files?

I am new to JS, and I need to load a file1, decompress a part of it to file2, and then make that decompressed file2 available to user's download--all completely browser-side (no Node.js etc.). For decompression I have: let fb; const decB = document.querySelector('button[id="dec"]') const inputB = document.querySelector('input[type="file"]') input.addEventListener('change', function(e) { const r = new FileReader() r.onload = function () { const archive = new Uint8Array(r.result, start, length) try { fb = pako.inflate(archive); } catch (err) { console.log(err); } } r.readAsArrayBuffer(input.files[0]) }, false) decB.addEventListener("click", function(e) { try { const t = new TextDecoder().decode(fb) console.log(t) } catch(err) { console.log(err) } }, false) I want to be able to access the contents of the resu

d3.js force directed tree matrix d3.js force directed tree matrix

In previous applications, I have adapted standalone trees into a "matrix" format that uses rows and columns to display many small hierarchical relationships in one visual. See: D3.js v5 - Swarm Tree - How to iteratively center swarms around tree nodes For the next project, I was attempting the same thing but with "force-directed-trees." Hit a roadblock though after I followed the identical procedure as above and no errors were thrown in the process. Snippet: var margins = {top:100, bottom:300, left:100, right:100}; var height = 600; var width = 900; var totalWidth = width+margins.left+margins.right; var totalHeight = height+margins.top+margins.bottom; var svg = d3.select('body') .append('svg') .attr('width', totalWidth) .attr('height', totalHeight); var graphGroup = svg.append('g') .attr('transform', "translate("+margins.left+","+margins.top+")"); var data = [ {name:"Comp