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 trialJoshua Moten
2,403 PointsArray Element Exercise
I took it upon myself to use some of the skills that I have learned to add/remove items to an arrary
//Program adds numbers to an empty list then deletes them one by one
let numbers = [];
for (let i=2; i<=10; i+=2) {
console.log(i);
numbers.push(i);
console.log(numbers);
}
let newNum = numbers.length
if (newNum === 5){
console.log(`List is full! Length: ${numbers.length}`);
while (numbers.length >= 0 ) {
let removedNum = numbers.shift();
console.log(`Number being removed: ${removedNum} `);
console.log(numbers);
if (numbers.length === 0) {
console.log('breaking loop');
alert('Array is empty now');
break
}
}
}
I was have an issue with my while loop until I realized I made an infinite loop by accident . I corrected it by with this
while (numbers.length > 0 )
and removing the below lines since it was no longer needed
if (numbers.length === 0) {
console.log('breaking loop');
alert('Array is empty now');
break
}
Let me know how I did in troubleshooting my code and going a step a little more than hardcoding the code
1 Answer
Rachel Johnson
Treehouse TeacherHey Joshua Moten , this is looking great!
You definitely found the reason behind the infinite loop, which is awesome. Often it is the SMALLEST thing, isn't it??