+ 2
Query: prime number or not
Look at my code pls... I tried to make a query where you type in a number and it outputs if it is a prime numer or not... x needs to be defined as integer number I guess but I don't know how... I just started with javascript so I know the code is might wrong in general. I would be very grateful if someone could help me with my code. https://code.sololearn.com/WDBeaUNRUBIM/?ref=app
8 Réponses
+ 16
Here is a better way.
https://code.sololearn.com/Wx071DYM163i/?ref=app
+ 14
Wait, JS has no "int". Ints and doubles all are "Number".
Instead of division use modulus("%") and check if it is 0. Also, check only for 2,3,5 and 7 not all nos.
And what are you doing with the variable x? Its value is "undefined". That way all numbers will be prime.
+ 14
Plus, you also need to check that the number itself is not 2 and 3 and 5 and 7
+ 13
i don't think the way you are identifying a number as prime is correct
//1 more thing if Integer/Integer is an integer in iavascript , then no point of this algorithm (it might not work) , U can use double & take %1 //if 0 comes , then it means its an integer , else have a decimal part
+ 8
var number = parseInt(prompt("Enter a number"));
+ 2
thx but that doesnt help my code :( I need x to be integer number... so when I type in a number at first place (for example: 12) I want it to check if the number divided by 2 or 3 or 4 or... equals a integer number ...
I dont think the problem is in the var number... because I type in that number anyway ..
+ 2
Maybe anyone can build me a short code of what I want to reach :)
I dont understand what I have to change in my code to get the solution I want..
I want to type in a number and the code should check whether its a prime number or not...
at the end the programm's output should be:
This is a prime number
or
This isn't a prime number
I would be very grateful if someone can create me a code for this !! thx
+ 2
wow thx a lot for those inputs ;)
thats exactly what I was looking for.. I have been learning javascript for only a week now.. so I am not used to all functions but you all helped me a lot.
Big thanks for writing that amazing code :) !!!
I love this community.. I hope I can help you some day too when I am better at js.
chris