How to sleep in for loop [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do I make a 5 second delay in following code before moving to the next input element:
var inputs = document.getElementsByClassName('_aj7');
for(var i = 0; i < inputs.length; i++) {
inputs[i].click();
}

If you want to introduce a delay between actions, you need to use something like setTimeout. For example:
function f() {
inputs[i].click();
if (++i < inputs.length) {
setTimeout(f, 5000);
}
}
var inputs = document.getElementsByClassName('_aj7');
var i = 0;
if (i < inputs.length) {
f();
}

Related

JS use push in the for and apply toUpperCase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
//I need to do this with for
var name = ["Jhon", "Anne", "Ewreck", "Nine"];
for( var i = 0 ; i < name.length ; i++ ){
name[i].push(name[i].toUpperCase());
console.log(name[i]);
}
Try this
let name = ["Jhon", "Anne", "Ewreck", "Nine"];
let nameUpper = name.map( x => x.toUpperCase() )
console.log(nameUpper);

How to calculate the correct characters the user typed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
[Ask]
anyone know the reference for a case like this?
the user must type the word: "learn" -> total character 5
while the user types instead: "l3arn"
well I want to count the right word only, so the result 4
from google not yet find a solution, maybe someone knows the keyword/reference for the problem.
I want to implement it in javascript
You want to calculate the number of characters in the correct position?
In that case, it's a simple solution.
Javascript example:
function countCorrectCharacters(expectedString, string) {
var count = 0;
var l = Math.min(expectedString.length, string.length);
for (var i = 0; i < l; ++i) {
if (expectedString[i] === string[i]) {
++count;
}
}
return count;
}
var str = "learn";
var userInput = "l3arn";
var userInputArray = userInput.split('');
var counter = 0;
for(var i = 0; i< "l3arn".lenth(); i++){
if(str.indexOf(userInputArray[i]) !== -1) counter++;
}
If I understand correctly you want to count the number of letters in a string? Use this:
function getNumberOfLetters(string) {
let match = string.match(/[A-Za-z]/g);
return (match && match.length) || 0;
}
console.log(getNumberOfLetters("learn")); // 5
console.log(getNumberOfLetters("l3arn")); // 4
console.log(getNumberOfLetters("12345")); // 0

Dont know why this variable will not work [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am currently using
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
document.getElementById(hexArray[i]).className='transtart ' + burnArray[i];
};
And this is working just fine, however when I change it to this:
var hexSelect = document.getElementById(hexArray[i]);
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
hexSelect.className='transtart ' + burnArray[i];
};
It no longer works. And I have no idea why.
Look at your top line:
var hexSelect = document.getElementById(hexArray[i]);
It wont compile, because you use the i-variable from the for-loop.
var hexSelect = document.getElementById(hexArray[i]);
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
hexSelect.className='transtart ' + burnArray[i];
};
In the first line of your code you are referencing the hexArray variable which at this point is undefined. The i variable at this point is also undefined.
To get your code working all you need to do is move your first line of code into the first line of the for loop below. Does that work?

What is the difference between `for` and `while` loops in my Script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a JavaScript function that collects a number of input tags into an array and then decides if one of them has been selected:
function validateAnswerSelected () {
var formValid = false;
var groupedInput = document.getElementsByName("choice");
var i;
// while (!formValid && i < questions[questionNumber].choices.length) {
// if (groupedInput[i].checked) {
// formValid = true;
// }
// i++;
// }
// for (i = 0; questions[questionNumber].choices.length; i++) {
// if (groupedInput[i].checked) {
// formValid = true;
// }
// }
if (!formValid) {
alert("select an answer");
}
}
When I uncomment the while loop the function works correctly. However when I uncomment the for loop groupedInput becomes undefined.
What is the difference here?
Edit: when I uncomment the while then I do initialise the i variable. The error I made in the question is just a typo.
Typo:
for (i = 0; questions[questionNumber].choices.length; i++) {
Should be:
for (i = 0; i < questions[questionNumber].choices.length; i++) {
// ^ You forgot the `i <`
You didn't assign a value for "i" before using a while loop.
Blockquote var i; //default is undefined
But, in for loop you have assigned inside of that.
Blockquote for (i = 0; questions[questionNumber].choices.length; i++) {
That's why it returns undefined.
Sollution:
var i = 0;
Now it works fine. :)

Undefined var error in an array iteration on a page using mootools [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
Why is this firing back an error of Microsoft JScript runtime error: 'selectedMakeModelYearItem' is undefined?
Mootools won't let me use a simple for...in for iterations.
I've looked at it 6 ways to Sunday so what the heck am I missing?
Because selectedMakeModelYearItem is undefined.
selectedMakeModelYearItems isn't, though.
Maybe you try to call this code berofe page is loaded. In this case select tag that you try to access don't rendered and cannot be accessed from JavaScript. You can try something like
window.addEventListener("load",
(function() {
return function setMakeModelYearFilter() {
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
window.removeEventListener('load', setMakeModelYearFilter, false);
}})()
, false);

Categories

Resources