- 3,001
- Dundee, Scotland
Hello everyone!
First post in the section since I started my course but I am having a bit of a problem finishing off the final pieces of some javascript code I've been asked to write for my course.
The main function is to create a menu (which works) and has 3 options with the program to run indefinetly until you press Option 3, I know this section works.
The sub task is to create a User ID from First Name initial and Surname and I have the prompts working for that but I cannot get the 'Output' to show when checking the 'Logs' in the browser.
The option for the Calculate Factorial works and ending program displays the correct 'message'.
My full code is written below so if anyone is able to spot what I am missing I would appreciate a reply! Thank you!
const EXIT_CODE = 3;
const MENU_OPTIONS = `
Please enter an option
1 Create Login ID
2 Calculate Number Factorial
3 End Program
`;
let userInput = "";
do {
userInput = prompt(MENU_OPTIONS);
//Remove trailing whitespace from input
userInput = userInput.trim();
if (userInput === "") {
console.error("Error: please enter a valid option");
//This makes the loop re-evaluate its condition, effectively restarting the loop
continue;
}
//Convert input to a number type
userInput = Number (userInput);
if (Number.isNaN(userInput)) {
console.error("Error: Input is not a number");
continue;
}
// Ensure input is an integer (no decimal place)
userInput = Math.trunc(userInput);
// Generating a User ID through Input
if (userInput === 1) {
let FIRST_INITAL = prompt("Please enter your First Name initial");
let SURNAME = prompt ("Please enter your Surname");
//Create User
function createUser(FullName) {
//Get First Name Initial
const FIRST_INITAL = fullName[0];
//Find Space
const SPACE_POS = fullName.indexOf(" ");
//Get Surname
const SURNAME = fullName.slice(SPACE_POS + 1);
//Create the UserName
const USERNAME = FIRST_INITAL + SURNAME;
//Display Username
console.log(
};
// program to find the factorial of a number
} else if (userInput === 2) {
// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));
// checking if number is negative
if (number < 0) {
console.log('Error! Factorial for negative number does not exist.');
}
// if number is 0
else if (number === 0) {
console.log(
}
// if number is positive
else {
let fact = 1;
for (i = 1; i <= number; i++) {
fact *= i;
}
console.log(
}
} else if (userInput < 0 || userInput > 3) {
//Explicit check if input is out of range.
//An else block would throw the error message
//below iif 0 (the exit code will be entered.
console.error("Error: Please enter a valid option")
}
} while (userInput !== EXIT_CODE);
console.log("Have a nice day! Goodbye!");
I feel it must be something 'obvious' I am missing in the code but I cannot see it for the life of me looking through the script and no errors are being shown either which isn't helping so again thank you for any replies!
First post in the section since I started my course but I am having a bit of a problem finishing off the final pieces of some javascript code I've been asked to write for my course.
The main function is to create a menu (which works) and has 3 options with the program to run indefinetly until you press Option 3, I know this section works.
The sub task is to create a User ID from First Name initial and Surname and I have the prompts working for that but I cannot get the 'Output' to show when checking the 'Logs' in the browser.
The option for the Calculate Factorial works and ending program displays the correct 'message'.
My full code is written below so if anyone is able to spot what I am missing I would appreciate a reply! Thank you!
const EXIT_CODE = 3;
const MENU_OPTIONS = `
Please enter an option
1 Create Login ID
2 Calculate Number Factorial
3 End Program
`;
let userInput = "";
do {
userInput = prompt(MENU_OPTIONS);
//Remove trailing whitespace from input
userInput = userInput.trim();
if (userInput === "") {
console.error("Error: please enter a valid option");
//This makes the loop re-evaluate its condition, effectively restarting the loop
continue;
}
//Convert input to a number type
userInput = Number (userInput);
if (Number.isNaN(userInput)) {
console.error("Error: Input is not a number");
continue;
}
// Ensure input is an integer (no decimal place)
userInput = Math.trunc(userInput);
// Generating a User ID through Input
if (userInput === 1) {
let FIRST_INITAL = prompt("Please enter your First Name initial");
let SURNAME = prompt ("Please enter your Surname");
//Create User
function createUser(FullName) {
//Get First Name Initial
const FIRST_INITAL = fullName[0];
//Find Space
const SPACE_POS = fullName.indexOf(" ");
//Get Surname
const SURNAME = fullName.slice(SPACE_POS + 1);
//Create the UserName
const USERNAME = FIRST_INITAL + SURNAME;
//Display Username
console.log(
Your username is ${USERNAME}
);};
// program to find the factorial of a number
} else if (userInput === 2) {
// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));
// checking if number is negative
if (number < 0) {
console.log('Error! Factorial for negative number does not exist.');
}
// if number is 0
else if (number === 0) {
console.log(
The factorial of ${number} is 1.
);}
// if number is positive
else {
let fact = 1;
for (i = 1; i <= number; i++) {
fact *= i;
}
console.log(
The factorial of ${number} is ${fact}.
);}
} else if (userInput < 0 || userInput > 3) {
//Explicit check if input is out of range.
//An else block would throw the error message
//below iif 0 (the exit code will be entered.
console.error("Error: Please enter a valid option")
}
} while (userInput !== EXIT_CODE);
console.log("Have a nice day! Goodbye!");
I feel it must be something 'obvious' I am missing in the code but I cannot see it for the life of me looking through the script and no errors are being shown either which isn't helping so again thank you for any replies!