2

Hello there I have been challenged to write a program in JavaScript despite not really knowing much about it that asks the user for a number and then calculates the factorial of that number. I used already asked questions and managed to get the calculation to work but couldn't get the required output. I have to get it in the following output without using any fancy libraries or extra variables/arrays (which I can't think of how to do) :

(assuming user input is 5):

The factorial of 5 is 5*4*3*2*1=120 
OR
5! is  5*4*3*2*1=120 

Here is the code I've got so far:

//prompts the user for a positive number
var number = parseInt(prompt("Please enter a positive number"));
console.log(number);
//checks the number to see if it is a string
if (isNaN(number)) {
  alert("Invalid. Please Enter valid NUMBER")
}

//checks the number to see if it is negaive
else if (number < 0) {
  alert("Please Enter valid positive number");
}

//if a positive integer is entered a loop is started to calculate the factorial of the number the user entered
else {
  let factorial = 1;
  for (count = 1; count <= number; count++) {
    factorial *= count;
  }

  //Sends the inital number back to the user and tells them the factorial of that number
  alert("The factorial of " + number + " is " + factorial + ".");
}

I know there are many similar questions to this as I looked around and used them to help me get this far but it is getting the output into the required format that I'm struggling with. I am told it is possible with a loop but don't know where to begin implementing that and I'm only allowed to use that solution.

Unfortunately this is part of a larger program in the challenge and I can only use the following variables:

Number (variable initialised as 0 to hold user input) Factorial (variable initialised to 1 to hold value of calculated factorial) Count (variable to hold number of times loop is executed for performing factorial calculation)

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Check the error in the console - you're trying to use an undeclared variable `num`. Either rename `number` to `num` or declare `num` (and assign `number` to it...) – VLAZ Nov 19 '20 at 15:54
  • _"without using any ... extra variables/arrays"_ - Why? That restriction doesn't make any sense (because you need at least one more variable to store either the result of the multiplication or the numbers from that multiplications) – Andreas Nov 19 '20 at 15:55
  • @VLAZ It was working I am just working from several versions I'll go run it now. –  Nov 19 '20 at 15:55
  • @Andreas I know that is why I am stuck the challenge is strange. –  Nov 19 '20 at 15:57
  • There is already a question like this. Look [here](https://stackoverflow.com/questions/3959211/what-is-the-fastest-factorial-function-in-javascript) – myjobistobehappy Nov 19 '20 at 16:14
  • 1
    No fair changing the rules when you already got answers. Please ask a new question with all of the requirements stated upfront. – Heretic Monkey Nov 19 '20 at 16:17
  • 1
    @myjobistobehappy Sorry I am new to Stack Overflow and JS which one are you pointing to specifically. –  Nov 19 '20 at 16:28
  • 1
    @HereticMonkey My apologies my intent was not to mislead I am just new to this. –  Nov 19 '20 at 16:28
  • https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp see this answer + the Array.join suggestion + Array.slice. Maybe you can take it from there? – dssjoblom Nov 19 '20 at 16:53

2 Answers2

1

Probably you just need to build a string in that loop (on top of calculating the actual value):

let input=parseInt(prompt("Number?"));
let output="";
let result=1;
for(let i=input;i>1;i--){
  result*=i;
  output+=i+"*";
}
console.log(input+"! is "+output+"1="+result);

The "no-array clause" in your task presumably means that you are not supposed to build an array and use join() on it, like

let arr=[1,2,3,4,5];
console.log(arr.join("*"));
tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

I have updated your code mainly here, Also make sure you are using the same variable num in your code and not number:

let factorials = [];
let result = 1;
for (count = num; count >= 1; count--) {
        result *=count;
        factorials.push(count);
}

//prompts the user for a positive number
var num = parseInt(prompt("Please enter a positive number"));
console.log(num);

//checks the number to see if it is a string
if (isNaN(num))
{
  alert("Invalid. Please Enter valid NUMBER")
}

//checks the number to see if it is negaive
else if (num < 0) 
{
  alert("Please Enter valid positive number");
}

//if a positive integer is entered a loop is started to calculate the factorial of the number the user entered
else {
    let factorials = [];
    let result = 1;
    for (count = num; count >= 1; count--) {
        result *=count;
        factorials.push(count);
         }
    
    //Sends the inital number back to the user and tells them the factorial of that number
    alert("The " + num + "! is " + factorials.join('*') + " is " + result + ".");
}
Aadil Mehraj
  • 2,586
  • 1
  • 7
  • 17
  • Hello and thank you for your answer. Yes this code works well but unfortunately the challenge and ultimatley my question is poorly written. I can only use: Number (variable initialised as 0 to hold user input) Factorial (variable initialised to 1 to hold value of calculated factorial) Count (variable to hold number of times loop is executed for performing factorial calculation) Otherwise yes I would use your solution. –  Nov 19 '20 at 16:06
  • If the solution helped do upvote the answer, thanks :) – Aadil Mehraj Nov 19 '20 at 16:16
  • 1
    @BigJohn Please ask a new question with all of your requirements stated upfront, in the question. If you want, accept an answer here. – Heretic Monkey Nov 19 '20 at 16:16