JS realizes the random arithmetic topic effect _JSP programming

Source: Internet
Author: User
Tags arithmetic gcd generator rand xmlns

This article mainly introduces the random arithmetic topic, thus can automatically generate the random 30 arithmetic questions. can generate random arithmetic topics for pupils to use, but still have problems, elementary school Ah! They do not know the negative number, do not know decimal. So we have to add a judgment condition. The specific code is as follows:

Switch (Arr[n]) {case
    "+": {
    Case "-": {
    while (sum<0) {
     var a = Getrandomnum (1,30);
     var B = Getrandomnum (1,30);
    Case "*": {
    Case "/": {
    while (Hasdot (sum)) {
      var a = Getrandomnum (1,100);
      var B = getrandomnum (1,100);

So there are no negative numbers or decimals.

However, the pupil seems to be a fraction of the operation. So I started trying to write points again.

<body> <button class= "btn btn-success btn-sm" type= ' button ' onclick= "Pl_f ()" > Start generating Topics </button> < 
  Span id= "Text_f" ></span> <script> function Getrandomnum (min,max) {var Range = max-min; 
  var Rand = Math.random (); 
 Return (Min + math.round (Rand * Range));
  function Max_num (x,y) {var t;
   while (y) {t = x%y;
   x = y;
  y = t;
 return x;
   The function Fenshu (a,b,p) {if (p==1) {if (a>b) {return + "/" +B;
   }else if (a<b) {return a + "/" +B;
   }else {return 1;
   }}else {var x=a/p;
   var y=b/p;
   if (y==1) {return x;
   }else{return x+ "/" +y; 
  }} function Pl_f () {var son_a = Getrandomnum (1,30);
  var par_b = Getrandomnum (1,30); 
  var Son_c = Getrandomnum (1,30); 
  var par_d = Getrandomnum (1,30);
  var x=son_a+ "/" +par_b;
  var y=son_c+ "/" +PAR_D;
  document.write ("First score:" +x+ "<br>");
  document.write ("Second score" +y+ "<br>");
  Max_x=max_num (Son_a,par_b); Max_y=max_num (SON_C,PAR_D);
  document.write ("The first gcd" +max_x+ "<br>");
  document.write ("The second gcd" +max_y+ "<br>");
  var N=fenshu (son_a,par_b,max_x);
  var M=fenshu (son_c,par_d,max_y);
  document.write ("The first fractional reduction:" +n+ "<br>");
 document.write ("Second fractional simplification" +m);
 } </script> </body>

This can dynamically generate two fractions, and then output the fractional molecule and the gcd of the denominator. The simplest formula for the final output molecule.

The final thing is to put the score together in an integer!

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

That's what happens when you finish the show.

The last answer and the topic separate version!

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.