Array.lenght returns undefined [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 8 years ago.
Improve this question
I wish to get the length of an array so I can use it further in another function but it returns undefined. This piece of code gets a file, opens it and splits it for each new line. I'm trying to get the length of the array but returns undefined.
function readBatFile(bfile){
var rawFile1 = new XMLHttpRequest();
rawFile1.open("GET", bfile, false);
rawFile1.onreadystatechange = function ()
{
if(rawFile1.readyState === 4)
{
if(rawFile1.status === 200 || rawFile.status === 0)
{
var allCode = rawFile1.responseText;
var ary = new Array;
ary = allCode.split(/.*\n/gm);
var rcount = ary.lenght;
document.getElementById('test').innerHTML = rcount;
}
}
};
rawFile1.send(null);
}

It is spelled length not lenght.
It should be:
var rcount = ary.length;

Related

tried reverse array but reverse last item instead [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 tried to reverse array items, but it reverse the last item not array. I also posted the expected output. Hope it will make easy to understand.
var color = ("red", "green", "blue");
function printReverse(str) {
for (var i = str.length - 1; i >= 0; i--) {
console.log(str[i]);
}
}
printReverse(color);
/*
output
e
u
l
b
*/
You define arrays with this statement: ["red","green","blue"], not this: ("red","green","blue"):
var color = ["red","green","blue"];
function printReverse(str){
for ( var i = str.length-1; i>= 0; i-- ){
console.log(str[i]);
}
}
printReverse(color);
var color = ["red","green","blue"];
var rev_color = color.reverse()

Why my if & else condition doesn't work correctly? [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
When I use this code to add & remove the style , Part ( else ) only works and sets (display) to block, But it will not return the class if clicked again.
what's wrong?
var navDropDown = document.querySelectorAll('.menu-item-has-children > a');
for (let i = 0; i < navDropDown.length; i++) {
navDropDown[i].addEventListener('click', (e) => {
if (navDropDown[i].nextElementSibling.style.display = 'none') {
this.nextElementSibling.style.display = 'block';
} else {
this.nextElementSibling.style.display = 'none';
}
})
}
The line:
if (navDropDown[i].nextElementSibling.style.display = 'none') {
uses a single =, instead of ==. In Javascript, = is an assignment, which returns the value assigned. 'none' being not empty, it is converted into true, and thus the else will never be executed.

Javascript returning a value from a function [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
Here is a simplified version of my function and calling it.
When i console log the gameOver it should be "Treasure" or Huntress or false
// calling the function
var gameOver = this._checkGameOver;
_checkGameOver(){
var round = 1;
var gold = this._deck.filter(x => x =='Treasure').length;
var trap = this._deck.filter(x => x =='Trap').length;
if(gold == 0){
return('Adventurer');
}
else if(trap == 0 || round == 5){
return('Huntress');
}
else{
return false;
}
}
I think you don't call the function.
Try gameOver() instead of gameOver
You're assigning the actual method to gameOver, not its return value. Make your function call as follows:
let gameOver = this._checkGameOver();

mutation(["hello", "Hello"]); - Return true if Hello is equal to hello but my code returns false? [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 7 years ago.
Improve this question
I am comparing t to u which returns the same string which is "HELLO" but my
code returns false instead of true. I have also tried the array.indexOf()
function and still the same result. Help please.
function mutation(arr) {
var uppercaseArray = arr.toString().toUpperCase().split(","),
t = uppercaseArray[0].toString();
u = uppercaseArray[1].toString(),
n = t.localeCompare(u);
if (n = 1) {
return true;
} else {
return false;
}
}
mutation(["hello", "Hello"])
if(n = 1) will always be true, I think you're looking for == or ===
Maybe you can change to if (n == 0) {
function mutation(arr) {
var uppercaseArray = arr.toString().toUpperCase().split(",");
t = uppercaseArray[0].toString();
u = uppercaseArray[1].toString();
var n = t.localeCompare(u);
if (n == 0) {
return true;
} else {
return false;
}
}
console.log(mutation(["hello", "Hello"]));
The localCompare function returns 0 when they are equal.

Why does `document.getElementById(“#datepicker1”)` not find my element? [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 7 years ago.
Improve this question
I am trying to show / hide a text input based on the selection of a drop down list.
Here’s a JSFiddle to what I am trying to do.
When I get into the function, I get a “TypeError: Cannot set property 'type' of null”.
This is the code:
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "text";
}
else {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "hidden";
}
};
You do not need the # when using getElementById. getElementById returns null if does not find the element. null does not have a property named type - therefore you get the error.
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "text";
} else {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "hidden";
}
};
y is null, because the id of the element is probably datepicker1 and not #datepicker1
var y = document.getElementById("#datepicker1");
should be
var y = document.getElementById("datepicker1");

Categories