Output Problem in Javascript

Silver_Wolf

Nice! Let's do it again when you level up!
Premium
2,971
Scotland
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(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!
 
I don't know Java, but shout this not have semi-colon on it?

It was always semi-colons when I did PHP/SQL.
Well done my man! I also just noticed my hideous spelling in the line when you quoted me, I will try that in a bit but that should fix it I reckon!
 
@Racing_Miku I'm not sure what the intention was with your createUser() function but it's not being called and doesn't really make sense in the context anyway. Was that leftover from something else or copied from somewhere? If your prompts work then just concatenate the 2 inputs and log it.

Also, javascript doesn't really care about explicit semicolons in a lot of cases (although I think for clarity and safety it's probably best practice to use them consistently). It's worth looking into how it will automatically try to insert them if you are unaware.
 
Last edited:
I think that may have been a left over, I will try and remove it and see if that makes a difference to the output, that may well be the main jist of the problem as this is the only part I am failing on, time for more work and testing!

Thanks for the reply @David
 
Back