Why doesn't array.push() work when looping object? [closed] - javascript

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.

Related

Separate array line by line [closed]

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]);

Issues with displaying certain array elements with a conditional in javascript [closed]

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 2 years ago.
Improve this question
I'm trying to use a for loop to display all the elements in my array that are greater than 5. I am using an if-statement to determine the values greater than five, but when I run the code, I get values printed to the console that are both below and over five. I have tried creating variables to store the index values and tried using the && operator, but none of these have worked.
Here is the code for reference:
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Original: " + myArray);
console.log("Values greater than 5");
// the part I have issues with
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] > 5) {
console.log(i);
}
}
You are printing i not the array element.
try
console.log(myArray[i]);

Regex Issue JavaScript [closed]

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 4 years ago.
Improve this question
I'm trying to take a string and check whether or not it contains a lowercase letter or number, and then if so push that letter or number to an array.
for(let i = 0; i < datearg.length; i++)
{
log.info(datearg.charAt(i));
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
}
However, both if statements always evaluate to true and the arrays end up containing every single character in datearg. Anyone know why?
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
is equivalent to
if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));
i.e. push is not conditional. This is the primary reason why many style guides heavily discourage control structures without braces (which only take a single statement).

Following a tutorial, cant figure out my javascript syntax mistake [closed]

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 5 years ago.
Improve this question
I want to change all instances of the word "cão" on a page to read "gato" (following an older tutorial).
All instances of "cão" are inside a span and I´m trying to change them by using getElementsByTagName method.
Since there more than one, I cant use innerHTML to change the content so I´m using a for loop to cycle through all positions but I´m getting a sytax error after the increment i++. Why is that?
var elementoHeading = document.getElementById('heading');
elementoHeading.innerHTML = "Tudo sobre gatos";
var nomesTags = document.getElementsByTagName("span");
for (var i = 0, i < nomesTags.length, i++) {
nomesTags[i].innerHTML = "gato";
}
Use semicolons, not commas, in the for construct:
for (var i = 0; i < nomesTags.length; i++) {
^ ^
The reason the syntax error is after the increment is because the JS engine expects 3 statements inside of the for's brackets, but you only gave one (commas don't terminate the statement).

Simple javascript for selecting random element out of array outputting "undefined" on 2 elements (jsfiddle included) [closed]

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

Categories