JS Linear Data Structures - Web navigator

The course is Pass the technical interview with Javascript, the chapter is Javascript Linear Data Structure (section Stacks), and the project is Web navigator. During the User Interface Part 1 there is a variable finish which is set to false, and then the while loop should (for the purpose of a simple user interface) console.log(instructions) while finish is false:

let finish = false;
let showBack = false;
let showNext = false;

showCurrentPage('DEFAULT: ');

while(finish === false){
let instructions = baseInfo;

if(backPages.peek() != null){
instructions = ${instructions}, ${backInfo};
showBack = true;
} else {
showBack = false;
}

if(nextPages.peek() != null){
instructions = ${instructions}, ${nextInfo};
showNext = true;
} else {
showNext = false;
}
instructions = ${instructions}, ${quitInfo};
console.log(instructions);
}

And then in logged instructions should be an option for user to quit the program by setting finish to true which should terminate this while loop. How can this be a good practice? The while loop goes infinitely because finish is always false! It should be true only when user quits the program which is an option he should get with this while loop, which is set to go until the end of the world because the finish is false and it will always be false until the user quits the program which is an option he should get with this while loop, which is set to go until… (?). What am I missing here?

https://www.codecademy.com/paths/pass-the-technical-interview-with-javascript/tracks/javascript-linear-data-structures/modules/stacks-js/projects/learn-stacks-javascript-web-navigator

I think I’m missing a break at the end of the while loop (?)

Hello, and welcome to the forums!

The option to quit (by setting finish to true) is covered in User Interface Part 2, specifically task #19. Is that what you mean?

Hi, thank you for the reply. I do mean the option quit, but the problem seem to be in while loop which goes first. After the while loop another portion of code gets executed (User Interface Part 2):

const answer = prompt(question);
const lowerCaseAnswer = answer.toLowerCase();

if((lowerCaseAnswer !== ‘b’) && (lowerCaseAnswer !== ‘n’) && (lowerCaseAnswer !== ‘q’)){
// create a new page
newPage(answer);
} else if((lowerCaseAnswer === ‘b’) && (showBack = true)){
backPage();
} else if((lowerCaseAnswer === ‘n’) && (showNext = true)){
nextPage();
} else if(lowerCaseAnswer === ‘b’){
console.log(‘There is no back page! The stack is empty’);
} else if(lowerCaseAnswer === ‘n’){
console.log(‘There is no next page! The stack is empty’);
} else if(lowerCaseAnswer === ‘q’){
finish = true;
}

And here the user should quit if he wants to, thus breaking the while loop (setting finish to true). But the while loop is triggered with finish=false and it goes infinitely for the reason I mentioned above.

Your code to evaluate the user responses should be in the while loop, so as soon as finish is set to true it will end the while loop the next time the condition is evaluated.

If I’m still misunderstanding, can you please post your complete source using this forum option:
image

My bad, I put user input processing outside of the loop. I guess I got confused with commented code division in the script.js. Anyway, the problem fixed. Thank you SO much.

1 Like

How did you fix this error? When I run my node script.js it just keeps running forever as well. Thanks for your help

Hi, did you put both User interface 1 and User interface 2 in the while loop (which loops until the finish is set to true)?

Wow that works! Thanks alot! Instructions were not clear that both parts had to be in the while loop! Thank you!!!

Yeah no problem, glad it works :slight_smile: