Query: prime number or not | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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

26th Feb 2018, 8:48 AM
Christian
8 Réponses
26th Feb 2018, 1:29 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 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.
26th Feb 2018, 12:56 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 14
Plus, you also need to check that the number itself is not 2 and 3 and 5 and 7
26th Feb 2018, 12:58 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 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
26th Feb 2018, 12:30 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
var number = parseInt(prompt("Enter a number"));
26th Feb 2018, 8:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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 ..
26th Feb 2018, 9:05 AM
Christian
+ 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
26th Feb 2018, 12:37 PM
Christian
+ 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
26th Feb 2018, 2:11 PM
Christian