access property in node.js [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
sorted = Object.keys(PLAYER_LIST).sort(function(a,b){return a.score - b.score}); // have key value
console.log(PLAYER_LIST[sorted[0]].team); // result:"A"
for(var loop=0; loop<=sorted.length; loop++) {
if(PLAYER_LIST[sorted[loop]].team == "A") { // error
some code...
}
}
When I sorting like this. console.log working well. But if sentence is not working. I receive error message. I don't know why. please help me.
if(PLAYER_LIST[sorted[loop]].team == "A") {
^
TypeError: Cannot set property 'team' of undefined

Your loop goes one step too far
for (var loop = 0; loop <= sorted.length; loop++) {
// ^^ here
An arrays length starts at zero when it's empty, and is 1 if the array contains one item.
If you have an array
var array = ['a']
arrays are zero based, so the first and only item is array[0], and the length is 1
When you iterate and you go all the way to the arrays length, you go one index too much, and you end up trying to get array[1], which doesn't exist.
What you wanted was
for (var loop = 0; loop < sorted.length; loop++) {...

Related

My for loop runs only once while trying to reverse an array 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 last year.
Improve this question
var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);
I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.
As #Barmar pointed out the problem is due to a logic error.
/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
for(let i = 0 ; i < size ; ++i)
list.push(window.prompt());
}
/* Print a list container */
function print(list){
for(let i = 0 ; i < list.length ; ++i)
console.log(list[i]);
}
var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);

Fatal error occurring in Javascript forming array [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 want to make a function that forms an array based on the user input so I write the javascript as below but it only returns a fatal error. What is wrong with this code? I try to match with the book's code but I don't find anything particularly different so I came to the StackOverflow. The code is as follows
function arrayForm(start, limit)
{
let array = [];
for (start <= limit; start++;)
{
array.push(start);
}
return array;
}
console.log(arrayForm(1,10));
try
{
let array = [];
for (let i = start; i <= limit; i++)
{
array.push(i);
}
return array;
}
console.log(arrayForm(1,10));

Why does a for loop behave oddly in a JavaScript constructor? [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 new to constructors - I'm trying to use one to create a customizable object, with this sort of code:
class test{
constructor(range) {
var start;
if(range==="a"){
start = 56;
}
else if(range==="b"){
start = 53;
}
for(var i=start; i<(start+5); i++); {
console.log(i);
//construct an array here
}
}
}
const myTest = new test("a");
But only the last loop seems to execute!
The log shows just the value 61.
You have an semicolon to early. The result is an empty statement and an additional block statement outside of the loop.
Finally you get the last value of i.
for (var i = start; i < (start + 5); i++); {
// ^

Cannot read property 'teacherId' of undefined when calling the array of object [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
var teacherArray=[];
I have created an array variable.
I am creating an array with the key and value. and pushing these data to the teacherArray.
random={
teacherId:TeacherId,
day:day,
periodCount:period,
class:Studentclass,
section:Studentsection,
startTime:schoolStartTime,
endTime:schoolEndTime
};
teacherArray.push(random);
console.log(teacherArray);
In the console I am able to see the created array. But when the submit button clicked i am calling the array like
teacherLength=teacherArray.length;
for (let k=0;k<=teacherLength;k++)
{
var teachId=teacherArray[k].teacherId;
console.log(teachId);
}
In the console displays the teacherId, but next line shows the error as
TypeError: Cannot read property 'teacherId' of undefined
Arrays are zero index based. So when you write k<=teacherLength you are requesting more than what array have. That should be changed to
k<teacherLength
Arrays are 0 indexed. You are having,
for (let k=0;k<=teacherLength;k++)
Make it,
for (let k=0;k<teacherLength;k++)
Your code is reading an extra item, you need to replace
for (let k=0;k<=teacherLength;k++)
with
for (let k=0;k<teacherLength;k++) //notice < instead of <=

Type error, cannot read property 'match' of undefined [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 got stuck in the Javascript code below, I don't know what's the problem.
the code is always showing"Type error, cannot read property 'match' of undefined",
Here IS THE CODE:
function keepletteronly(str) {
str=str.toLowerCase();//Make the string to lower case
arr=str.split(""); //This way I make an array out of the string
arr1=[]; //make an new array
for (i=1; i<=arr.length;i++){
if (arr[i].match(/[a-z]/)!=null) { //This is where the problem is
arr1.push(arr[i]); //retain only the letters and append to the arr1
}
}
newstring=arr1.join;
return newstring;
}
keepletteronly("1eye");
The index of a JS array starts from zero.
You should change your for sentence to
for ( i = 0; i < arr.length; i += 1) {
Moreover, another sentence is also incorrect:
newstring=arr1.join;
it should be
newstring=arr1.join('');
or simply produce the resultant string without arr1, like:
newstring='';
...
newstring+=arr[i];
Arrays in Javascript are base 0, meaning the first item is at index 0. Imagine you have three items in an array, the length property will say 3 and the three items will occupy indices 0, 1 and 2.
In your for loop, you start at index 1 (i=1;), meaning the loop will skip the first item (at index 0) and then you iterate while i is lower or equal to the length of the array. The length of the array is 3, but because of base 0, the last item in the array is at index 2. So in the last iteration of your for loop, you try to access index 3 of the array, which is undefined.
So, what you need to do is to change your for loop expressions to look like this:
(i = 0; i < arr.length; i++)
This way you will properly iterate over 0, 1 and 2 instead of 1, 2 and 3

Categories

Resources