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]
}
Related
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 1 year ago.
Improve this question
Can someone please show me how to sort this
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"]
into this
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/8/video-109.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4"]
it should be sorted based on last numbers in string before .mp4 but it doesn't work
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"
]
videos.sort(function(a, b) {
var spix = a.split('-')[1];
a = parseInt(spix.split('.mp4')[0]);
var spixb = b.split('-')[1];
b = parseInt(spix.split('.mp4')[0]);
return a - b;
});
console.log(videos);
videos.sort((a,b) => a.split('-')[1].slice(0,-4) - b.split('-')[1].slice(0,-4));
console.log( videos)
Basically I'm just splitting it by the "-" then taking out the '.mp4' and sorting it
Your issue is with the defenition of variable b. You defined it incorrectly as b = parseInt(spix.split('.mp4')[0]); it should be b = parseInt(spixb.split('.mp4')[0]);
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"];
videos.sort(function (a, b) {
var spix = a.split('-')[1];
a = parseInt(spix.split('.mp4')[0]);
var spixb = b.split('-')[1];
b = parseInt(spixb.split('.mp4')[0]);
return a - b;
});
console.log(videos)
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
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]);
}
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?
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.