If button is clicked and arrays match, run function in javascript - javascript

This is what I am trying to achieve: If a input image is clicked, and the input image matches word (referenced in array) run a previously ran function again.
I've messed around with the code but can not seem to figure out why it's not working.
I'm new enough to javascript so please excuse that lack of knowledge and terminology :P

You need to use array.indexOf(element)
Checkout this question
How do I check if an array includes an object in JavaScript?
Update: for your case yo better use array.some
randomwodrz.some(function(item){return item.word === 'word_to_match'})

Related

How to see size of voice channels that my bot is currently in (joined)?

How could I add the current size of voice channel connections to my /info command? I tried using console.log and logging client.voice.adapters, but still didn't manage to figure it out.
I know that this is a simple question, but maybe answers from you guys will help others, thanks!
I didn't manage to find an answer to my question, but I came up with a solution. I found a way that works for me and is easier than looking for code chunks or asking questions.
Here's a tip: If you make silly mistakes as I did, check the docs.In this case ( Discord.js.org and MDN Web Docs ).
What did I do / how to?
Create a new Map();
Use <Map>.set and <Map>.delete to add and remove a number.
Use else if(newState.status === "idle") { 4th step } to check if the new state status is equal to Idle.
Add the <Map>.delete(interaction.guild.name, <numberToRemove>); function into 3th step function to remove one number from the map.
Copy and Paste if you are too lazy:
Function for adding a number to map should look like this: <MapName>.set(interaction.guild.name, <numberToAdd>);Function for deleting a number from map looks like this: <MapName>.delete(interaction.guild.name, <numberToRemove>);
client.voice.adapters maps guild ids to voice adapters. It returns a Map and Maps have a size property that returns the number of elements in that Map.
Would this work?
client.voice.adapters.size

need direction on how to update this code, language, tutorial?

I can usually Google and find my answer, but I have no idea with this issue on where to even start. If someone could point me in the right direction here as to what language and syntax is being used here, I'd appreciate it.
This is saying "if input1 = 'option1', then output text1 into span1 and if not, input text2.
I'm sorry this is so basic, but need to learn how to edit this but I'm lost without a tutorial or guide.
$('#span1').html(input1='option1'?'text1':'text2');
This is kind of a repost from an earlier question I had where I was accused of trying to get free coding advice so I've made this as basic as possible. I'm not looking for free coding here - if someone could give me a hint as to where I could find more information, I'd appreciate it.
This is a snippet of jQuery - a javascript framework.
$('#span1').html(input1='option1'?'text1':'text2');
$ This is the jQuery "object", and passing a selector argument to it will return a jQuery-wrapped object that you can chain methods to.
#span1 selector argument (may as well be CSS)
input1='option1'?'text1':'text2' Will output "text1" or "text2" depending on wether or not the variable option1 evaluates to true. It's very likely, as u_mulder points out, that this is going to fail. Use two == or three === for comparisons!
html(...) The html method chained onto the output of the selector accepts the new value to write to that element's contents in the DOM.
Suggested reading:
What is a jQuery Object?
Which equals operator (== vs ===) should be used in JavaScript comparisons?
http://api.jquery.com/html/
https://api.jquery.com/category/selectors/
Was able to do it not using that code but a code I'm more familiar with like this:
if(pay_type == 'onetime') {
$('#bank_card_pay_type').html('one time payment');
}
else if(pay_type == 'montly') {
$('#bank_card_pay_type').html('3 monthly payments');
}
else if(pay_type == 'monthly12') {
$('#bank_card_pay_type').html('12 monthly payments');
}

Javascript: Array not shifting, multiple setTimeout functions (JSON)

I'm really stuck on this javascript question!
So I'm making a web page that will be completely animated (so it can be used for display for example in a television). That animation will be configurable by the user (stored in a database).
Right now, I've already made the code to store the configuration and to get the configuration (I do an AJAX call and save the configuration in an array of json objects) and everything is as it should be.
The problem is in the animation in which I go through the array and use setTimeout function to create animations. To iterate through the array I rotate it
(I use array.push(array.shift()) according to the answer here).
The first time the intervalmaster function is used, everything goes according to plan, but when the function is called again I need to rotate the array once more (because of the last animation) and the array just doesn't rotate!
Bellow I've left a portion of the code that I'm using that reproduces the problem I'm getting. I've also added the array jsonanima with some possible values (In reality the array is probably much bigger and with higher values).
I really don't understand what is happening, I've also considered that this could be a problem of the multiple setTimeout functions because I've read somewhere (couldn't find the link, sorry!) that is not really advised to use multiple setTimeout.
If that's the case is there any other way to do this?
Thank you in advance!
EDIT: Thanks to the comment from mplungjan I've realized that if change the console.log(jsonanimate) to console.log(JSON.stringfy(jsonanima)) it outputs the correct values (the json array rotated). This got me even more confused! Why do I need to JSON.stringfy to get the array in the correct order?!
Anyway, can't test this with the full code now as I'm not in the office, tomorrow I'll give more feedback. Thank you mplungjan.
EDIT2: Finally solved my problem! So the thing was the call to the function recursivegroup (recursivegroup(0);), this call was made before I rotated the array, so when restarting the animation the array would still have the incorrect values and every sub-sequential value was wrong.
A special thanks to mplungjan and trincot for the comments that helped me debug this problem.
Bellow I leave the code corrected so anybody with the same problem can check it out.
jsonanima=[{"VD":5,"A":10,"diff":0.25},{"L":7,"IE":8,"diff":0.25}];
function intervalmaster(flag){
function recursivegroup(index)
{
if(index==0)
{
//animateeach(jsonanima,0);
}
setTimeout(function(){
//HERE IT WORKS
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
//animateeach(jsonanima,0);
//removed the if statement, since it was irrelevant as mplungjan noted
recursivegroup(index+1);
},(jsonanima[0]['diff'])*60*1000);
}
//Changed this
//recursivegroup(0);
var mastertime=0;
for(var key in jsonanima)
{
mastertime+=(jsonanima[key]['diff']);
}
console.log(mastertime,flag);
console.log(JSON.stringify(jsonanima));
if(flag==true)
{
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
}
//changed to here
recursivegroup(0);
masterinterval=setTimeout(function(){intervalmaster(true)},mastertime*60*1000);
}
intervalmaster(false);

filterDescendants and findDescendant with slate.js

I'm working on making a wysiwyg editor using slate.js
I'm in a situation where I'm trying to find the first node with text.
This picture below shows what I'm talking about:
Slate.js find first text pic
In my picture, I'd want to find the node that contains "this is my title.", even if there's several empty lines before it.
Basically if I have a bunch of text written in the editor, how do I find the first text that's not an empty string?
Looking through the docs, I've found the filterDescendants and findDescendants functions which seem to do what I'm looking for.
However, I'm unclear how to use them.
I've tried something like this:
this.state.state.startBlock.findDescendant((d) => d.text !== "")
But this just returns null
The docs say that findDescendant will "Deeply find a descendant node by iterator", where iterator is a function, but there's no examples provided for what sort of function you'd pass here.
Does anyone have any ideas or examples?
Slate.js author here.
You'll like want to do something like:
state.document.getBlocks().find(block => block.text != '')
This will search through the leaf block nodes in the document (in this case your paragraphs, headers, etc.) and find the first one that isn't empty.
The Slate data model is built with Immutable.js, so reading up on how that library works is very helpful for using Slate. In this case getBlocks() returns an immutable List, which has a find method.
Hope that helps!

How to find document element by regexp?

for example I have many input elements with name "test_(1...100)". I want to find all of them.
I just so happened to have had the exact same problem, this is how I solved it:
var divs=document.getElementsByTagName("div");
var re = /test_[\d]+/i;
for (thediv in divs) {
if (divs[thediv].id != null && divs[thediv].id.match(re)) {
//your stuff here
}
}
And as slhck pointed out, to make the chance of getting an answer bigger you should start accepting some answers to your questions. This is done by using the Green checkmark next to every answer.
Use RegularExpressions. An online tool might help.
The expression you need will be something like:
test_[\d]+
If you are using jQuery, then you can use .filter() with a function:
$('input').filter(function(){
return this.name.match(/^test/);
}).css({background: 'red'});
See DEMO.
If you are not using any library that would help you with that then you have to iterate over every input element that you find and match its name with your regex on every iteration.

Categories

Resources