Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I scrape info from webpage and I want to push them to array in order to use them later. But when I try to reach 1st, 2nd etc... item, instead of the word I got back only a character.
var arrType = [];
$('[name="type"]> option').each(function () {
arrType.push($(this).text());
});
const vehicleType = arrType.join(", ");
If I print the vehicleType then I got something what is looks like array (also the typeof is array), but when I want to print out vehicleType[0] I just get back one character.
console.log (vehicleType)
[text1,text2,text3]
console.log (vehicleType[0])
t
First, you can reduce your code a bit. You can define your variable at the same time you pull the option text by using .map() instead of .each().
var arrType = $('[name="type"]> option').map(function() {
return $(this).text().trim();
}).toArray();
Second, to target the first item of the array, don't use .join() at all.
console.log(arrType[0]);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
So I have a function that will be called with a 1 word string.
Test("ABCD")
my code:
function Test(x)
var str=""
str.push($)
x.toLowerCase()
return str
//So I want the output to change the "ABCD" into "abcd$"
You are accepting the argument x in the function and running toLowerCase on it. The output is not having any value from the input.
Also there is no push method defined for string. It can be String.concat. Even that is not needed, you could make use of arithematic + itsel for concatenation or string leterals.
Just lowercase the input append a $ symbol. Its done!!
Im making use of .toString() method aswell. To ensure code is not breaking for other input types
It should be
function Test(x) {
return x.toString().toLowerCase() + '$'
}
console.log(Test("ABCD"));
Or Simply.
Test = (x) => `${ x.toString().toLowerCase() }$`;
console.log(Test("ABCD"));
This is just an example, please change the text which you want to add at end as you wish.
I passed the string in function through console.log but you can call the function as you wish without console.log.
function capFirst(str) {
return str.toLowerCase().concat('', '$');
}
console.log(capFirst('ABCD'));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Below is my code, for some reason it outputs undefined for ['6'] & ['7'], all the other ones work. I don't understand what is going wrong.
var array = [
['1'],
['2'],
['3'],
['4'],
['5'],
['6']
['7'],
['8'],
['9'],
['10']
];
if(document.getElementById("random-element")) {
var rand = array[Math.floor(Math.random() * array.length)];
document.getElementById('random-element').innerHTML = rand;
}
https://jsfiddle.net/ggky7a03/
You missed a comma in your array, which will explain why those 2 values are not returning (they don't exist)
Side note, array is a reserved word in JS, so you can't (shouldn't) use it for your variable name, so change it to
var myAwesomeArray = [
// or similar
Here's your fixed code
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I'm doing a simple Sudoku solver with JavaScript, and there is one problem with adding new values to array. My script makes random length arrays in for..in loop. I have tested this script with Chrome Debugger, and there I see that it loops right count. Did I missed some important point of JS objects, or is the .push() wrong way to do this kind of thing?
this.areaSize = gridSideSize * gridSideSize;
//On progress -data structures
this.structures =
{
rows: new Array(),
columns: new Array(),
parents: new Array()
};
//Fill the data structures with the area object
for(var struct in this.structures)
{
for(var a = 0; a < this.areaSize; a++)
{
var tmp = new PartialArea(this.areaSize);
this.structures[struct].push(tmp);
}
console.log(struct.length);
}
Console tells me, that the first array is 4 items long and the second and the third one are 7 items long.
struct are the property names rows (which is a string of length 4), columns (7) and parents (7).
Logging this.structures[struct].length would give the expected results.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I was testing my answer to another SO question and came across this weird behavior, for the life of me I don't know what is up.
Code:
function translateLetter(input) {
const untranslated = "abcdefghijklmnopqrstuvwxyz";
const translated = "zyxwvutsrqponmlkjihgfedcba";
var i = untranslated.indexOf(input);
console.log(i);
return translated.substring(i,1);
}
console.log(translateLetter("a"));
console.log(translateLetter("b"));
console.log(translateLetter("c"));
Expected output:
0
z
1
y
2
x
Actual output:
0
z
1
<--- WTH?
2
y <--- WTF?
Code on JSFiddle
If speed is important, I'd use an object to do your lookup.
eg.
var translateLetter= {a:'z',b:'y'... etc }
and then you can simply do ->
console.log(translateLetter['a'])
Use .substr() for a length. .substring() takes a position.
The first empty space is a null because you have passed "b" in the function on which indexOf function returned 1 which is then set to variable "i" and i is then used in substring function and substring fuction return the value which is in the middle including the first and excluding the last index of the values given so substring got (1,1) and 1,1 are pointing the same index so it returned null.
same for the second.
substring(Begin index inclusive,last index exclusive);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
So I am using the following jQuery to get a count of all items in HTML with a class containing box. However it is not returning correctly, I feel like I'm missing something stupid.
var counter = $("*[class^=box]").length;
JSFiddle for example: jsfiddle
This link should return 1, but it returns 0.
EDIT:
It was a stupid mistake:I was previously using ID which was only going to be box1,box2,box3,etc so it made sense then. I knew it was stupid.
var counter = $("*[class=box]").length;
The "attribute starts with selector" matches the attribute, not neccesarely the classname
That means that this will match the selector
<div class="box0 emailbodytext" ...
as the attribute starts with "box", while this won't
<div class="emailbodytext box0" ...
as the attribute does not start with "box", even if one of the classes do
Here's one way to solve it
var counter = $('*').filter(function() {
return this.className.split(/\s+/).filter(function(klass){
return klass.indexOf('box') === 0;
}).length;
}).length;
FIDDLE
Your selector basically checks for elements that their class attribute starts with 'box'.
Your have more than one class name in your class attribute so you need to use 'contains' instead of 'starts with'.
Example:
var counter = $("*[class*=box]").length;