Hello everyone,
I am doing the Full Stack Engineer Path but not in order. I am now on the Technical Interview Module, Linear Algebra, where I have completed the Web navigator Project.
I am a small bug though:
Whenever I go back or forth, the instruction nextInfo and backInfo just continue to add up, and now I have this:
"NEW:
Current page = r
Back page = t
Next page = null
Enter an url, Q|q for quit, B|b for back page, Q|q for quit, B|b for back page, Q|q for quit, B|b for back page, Q|q for quit, B|b for back page, Q|q for quit, B|b for back page, Q|q for quit, B|b for back page, Q|q for quit
Where would you like to go today?"
Because we are constantly checking if the showBack or showNext are true, thus reactivating the if statement, and thus constantly adding the infos to the instructions variable.
How do you solve this kind of problem ? I think about adding a new conditionnal to check if the infos are already in the instructions or not, but is there more elegant ways to do it ?
here my Code:
const Stack = require('./Stack.js');
const prompt = require('prompt-sync')();
// ------------------------------
// Initialization
// ------------------------------
const backPages = new Stack();
const nextPages = new Stack();
let currentPage = 'Start Page';
// ------------------------------
// Helper Functions
// ------------------------------
const showCurrentPage =(action) => {
console.log(`\n${action}`);
console.log(`Current page = ${currentPage}`);
console.log('Back page = ', backPages.peek());
console.log('Next page = ', nextPages.peek());
};
const newPage =(page)=>{
backPages.push(currentPage);
currentPage = page;
while(!nextPages.isEmpty()){
nextPages.pop();
}
showCurrentPage("NEW: ");
}
const backPage = () => {
nextPages.push(currentPage);
let removedValue = backPages.pop();
currentPage = removedValue;
showCurrentPage("NEW: ");
}
const NextPage = () => {
backPages.push(currentPage);
let removedValue = nextPages.pop();
currentPage = removedValue;
showCurrentPage("NEW: ");
}
/*
* The following strings are used to prompt the user
*/
const baseInfo = '\nEnter an url';
const backInfo = 'B|b for back page';
const nextInfo = 'N|n for next page';
const quitInfo = 'Q|q for quit';
const question = 'Where would you like to go today? '
// ------------------------------
// User Interface Part 1
// ------------------------------
let finish = false;
let showBack = false;
let showNext = false;
let instructions = `${baseInfo}`;
showCurrentPage('DEFAULT: '
);
while(finish === false){
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);
// ------------------------------
// User Interface Part 2
// ------------------------------
const answer = prompt(question);
let lowerCaseAnswer = answer.toLowerCase();
if ((lowerCaseAnswer !== 'b') && (lowerCaseAnswer !== 'n')&&(lowerCaseAnswer !== 'q')) {
newPage(answer);
}
else if((lowerCaseAnswer === 'b') && (showBack === true))
{
backPage();
}
else if(lowerCaseAnswer ==="b"){
console.log("You didn't visit any pages yet, you can't go back.");
}
else if((lowerCaseAnswer === 'n') && (showNext === true))
{
nextPage();
}
else if(lowerCaseAnswer ==="n"){
console.log("You didn't visit any pages yet, you can't go foward.");
}
else if(lowerCaseAnswer === "q"){
finish = true;
}
}