using a var and a string to find a folder [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
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.

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

How To Do This in JavaScript [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 5 years ago.
Improve this question
var sunny=[1,2,3];
var bunny=[4,5,6];
var name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(name[i]);
}
//If User input Sunny then array elements of sunny will be printed.
You can use object for users and write your users in it, example:
var users = {
sunny: [1,2,3],
bunny: [4,5,6]
}
var name = prompt("Enter Name");
console.log(users[name]);
//If User input Sunny then array elements of sunny will be printed.
I think what you want is to use an object:
const names = {
sunny:[1,2,3],
bunny:[4,5,6],
};
const name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(names[name][i]);
}
There is not clean way to get a variable if you have its name as string. However its easy to access an objects properties with a string with the [] syntax.
U can achieve this by using 'eval'.
See the update code.
var sunny=[1,2,3];
var bunny=[4,5,6];
var name=prompt("Enter Name");
for(var i=0;i<3;i++)
{
document.write(eval(name)[i]);
}
You can use a switch-statement (https://www.w3schools.com/js/js_switch.asp) like
var a;
switch(name) {
case "sunny":
a = sunny;
break;
case "bunny":
a = bunny;
break;
}
for(var i = 0; i < a.length; i++) {
document.write(a[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]
}

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?

Grab airport name based on airport code from json data [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
Working on a flight booking website. Have a json file which contains the airport codes, city name and country name.
[{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}]
Now if my iata airport code matches to UTK i want to return the name.
Use filter() to find the object within the Array by its iata:
var arr = [{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}];
function findByCode(arr, iata){
var filtered = arr.filter(function(e){return e.iata = iata});
return (filtered.length == 1) ? filtered[0]:undefined;
}
console.log(findByCode(arr,"UTK").name);
JS Fiddle: http://jsfiddle.net/dE9nP/
$string = '[{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}]';
$data = json_decode($string);
echo count($data);
for($i=0;$i<count($data);$i++){
if($data[$i]->iata == 'UTK') echo $data[$i]->name;
}
you can use file_get_contents if the data in a file instead of $string;
This is basic searching! You need to loop over the array, and check each item for the matching code, then return the result. Here is a function:
function findAirport(myArray, iata){
for(var i = 0; i < myArray.length; i++){
var item = myArray[i];
if(item["iata"] === iata){
return item;
}
}
return null;
}
You can use that function like so:
var airports = [{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}];
var match = findAirport(airports, "UTK");
if(match){
console.log("name = " + match.name);
}
Just to highlight my preference to using simple for loops over other functions such as filter, here is a performance test comparing my answer to the other one that uses filter. I know it's meaningless in most real use cases, but I wanted to keep a reference to the jsperf link :)
NOTE: This answer was provided before the question tags were edited. At the time of posting this question was asking for a javascript solution

Categories

Resources