Dont know why this variable will not work [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 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?

Related

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

Javascript - Passing from a DOM element to array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hello and thank you for your time.
Here is the code :
<script>
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0, c = names.length ; i < c ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);// the results are : undefined
}
</script>`
I've tried to use the method tostring, or to push the results into the array but without success.
Thanks
Your main issue seems to be fixed. Make sure the DOM has been loaded before you try to run your code, and there is no need for two variables in your loop. Simplify it like below:
window.onload = function () {
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}
};
Fiddle
ar.length equals 0, because you just declare the array, but dont put anything into it. I think what you wanted to do is the following:
var names = document.getElementsByClassName('xx');
var ar = [];
for (var i = 0 ; i < names.length ; i++) {
ar[i] = names[i].innerHTML;
alert(ar[i]);
}

Why doesn't my javascript regexp work with array items? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
var x = new Array('1','2','3');
var y = new Array('a','b','c');
var iLen = x.length;
var s = 'abcdefgabcdefg';
for (var i=0;i<iLen;i++) {
var re = new RegExp(x[i],'g');
s = s.replace(y[i], re);
}
alert(s);
I want the result to be 123defg123defg.
Instead, I get /1/g/2/g/3/gdefgabcdefg.
You are doing it wrong, because you want to replace occurrences of array y to occurrences of x globally, you should say like
for (var i=0;i<iLen;i++) {
var re = new RegExp(y[i],'g'); //this is regexp for global y[i]
s = s.replace(re, x[i]); //replace all occurrences of y[i] with x[i]
}

using a var and a string to find a folder [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
to say some space and repetition I'm trying to use a var n to store a common location for a folder but also want to later combine a image name.
example.
var n="/me/work/air/image/";
var get_images = ["'+n+'" "1.jpg"",n+ "2.jpg" ]
I'm simply just trying to include var n with the string "1.jpg"
so the result would be
"/me/work/air/image/1.jpg","/me/work/air/image/2.jpg"
Are you looking for something like this?
var n= "/me/work/air/image/";
var get_images = [];
for (var i=0; i < 30; i++) {
get_images.push(n + "" + i + ".jpg");
}
this while loop is prolly the fastest one... length is the number of images ..
var path='/me/work/air/image/',length=10,array=[];
while(length--){
array[length]=path+length+'.jpg'
}
array.shift() // if you start from 1
or just create a function where i is the number of the image
function getImg(i){
return '/me/work/air/image/'+i+'.jpg'
}
use
var image1=getImg(1);
or
'<img src="'+getImg(1)+'">'
it also all depends inwhich contest you wanna reuse the image later and how often
Try this
var n= "/me/work/air/image/";
var get_images = {
first: n + "1.jpg",
second: n+ "2.jpg"
};
Now you can use get_Images["first"] or get_Images.first to get the data of each properties of JSON object.

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