Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
brandonlind2
7,823 Pointsdoes anyone know why my todo list is printing out numbers?
It's printing out numbers instead of the items in the dailyToDo variable and I'm not sure why.
var dailyToDo=[];
var completed=[];
console.log("Add a single item to the to do list or add multiple ones seperated by commas. Type 'exit' to exit, 'remove' to remove an item, 'complete' to add an item to the completed list' and 'show completed' to show completed list ");
while(true){
var userInput= prompt('+').split(',');
for(item in userInput){
if(item[0]==' '){
newItem= item.substring(1,item.length);
item=newItem
}
}
dailyToDo+= userInput;
if(userInput.includes('exit')){
break;
}
else if(userInput.includes('remove')){
var removeInput=prompt('what idea do you want removed? ');
var removedItem=dailyToDo.indexOf(removeInput);
dailyToDo.splice(removedItem,1);
continue
}
else if(userInput.includes('complete')){
var completeInput= prompt('What task did you complete? ');
var completeIndex= dailyToDo.indexOf(completeInput);
completed+=dailyToDo.splice(completeIndex,1);
continue
}
else if(userInput.includes('show completed')){
for(item in completed){
console.log(item);
}
continue
}
else{
for(item in dailyToDo){
console.log(item)
}
}
1 Answer
Steven Parker
243,658 Points
Your for...in loop is generating the numbers.
A for...in loop iterates the properties (not the values) of an object. In the case of an array, the properties are the index numbers. So to get to the values, you would need to use your iterator as an index into the array; or instead use a for...of loop, which iterates the values directly.
Also, MDN advises that "for...in should not be used to iterate over an Array where the index order is important."