Skip to main content

Create Marksheet Program in React JS, REACT JS Marksheet Program using controlled component


 Create Marksheet Program in React JS, REACT JS Marksheet Program using the controlled component.

WAP to create a mark sheet using five different subjects with the following conditions?

1)  all subject marks should be 0 to 100?

2)  If only one subject mark is <33 then the student will supply.

3)  If all subject marks are>33 then the student pass and displays division with percentage?

4)  if the minimum of two subject marks is <33 then the student will fail.

5)  if the student is suppl and the mark is >28 then five bonus marks will be added then the student will pass by grace and grace subject name.

6) display distinction subject name

7) display suppl subject name

Solution of Marksheet Program in React:-

Code of MarkSheet.js 

import React from "react";

export class Marksheet extends React.Component
{
    
   constructor()
   {
    
    super();
    this.state = {m1:'',m2:'',m3:'',m4:'',m5:'',res:''}
    this.handleSubmit1 = this.handleSubmit1.bind(this); 
    this.handleSubmit2 = this.handleSubmit2.bind(this);   
    this.handleSubmit3 = this.handleSubmit3.bind(this);   
    this.handleSubmit4 = this.handleSubmit4.bind(this);   
    this.handleSubmit5 = this.handleSubmit5.bind(this);   
    this.calcmarks = this.calcmarks.bind(this);
   }
   handleSubmit1(event)
   {
       this.setState({m1:event.target.value});
   }
   handleSubmit2(event)
   {
       this.setState({m2:event.target.value});
   }
   handleSubmit3(event)
   {
       this.setState({m3:event.target.value});
   }
   handleSubmit4(event)
   {
       this.setState({m4:event.target.value});
   }
   handleSubmit5(event)
   {
       this.setState({m5:event.target.value});
   }
  calcmarks(event)
  {
    var { m1,m2,m3,m4,m5,res} = this.state;
    var c=0;
    var s="";
    var m1 = parseInt(m1);
    var m2 = parseInt(m2);
    var m3 = parseInt(m3);
    var m4 = parseInt(m4);
    var m5 = parseInt(m5);
    if((m1>=0 && m1<=100) && (m2>=0 && m2<=100) && (m3>=0 && m3<=100) && (m4>=0 && m4<=100) && (m5>=0 && m5<=100))
    {
          if(m1<33)
          c++;
          if(m2<33)
          c++;
          if(m3<33)
          c++;
          if(m4<33)
          c++;
          if(m5<33)
          c++;

          if(c==0)
          {
             var per = (m1+m2+m3+m4+m5)/5;
             if(per>33 && per<45)
             {
                s = 'Congrats you are pass with third division'+per" %";
             }
             else if(per<60)
             {
                s = 'Congrats you are pass with second division'+per" %";
             }
             else 
             {
                s = 'Congrats you are pass with first division'+per" %";
             }
          }
          else if(c==1)
          {
              s = "SUPPL";
          }
          else{
              s"FAIL";
          }
      this.setState({res:"result is "+s})
    }
    else
    {
        this.setState({res:"Entered Marks is Invalid"})
    }
    event.preventDefault(); 
  }
   render()
   {
   
    return(
      
       <form onSubmit={this.calcmarks}>  
            <h1>controlled Form Example</h1>  
            <p>Mark1:  
                <input type="text" onChange={this.handleSubmit1}  />  
            </p>  
            <p>Mark2 :  
                <input type="text" onChange={this.handleSubmit2}   />  
            </p>  
            <p>Mark3 :  
                <input type="text" onChange={this.handleSubmit3}   />  
            </p>
            <p>Mark4 :  
                <input type="text" onChange={this.handleSubmit4}   />  
            </p>
            <p>Mark5 :  
                <input type="text" onChange={this.handleSubmit5}   />  
            </p>
            <p>
            <input type="submit" value="Calculate Marks" /> 
            </p>
            <p> {this.state.res}</p>

        </form>     
          

       )
   } 

}


Code of index,js file

  import React from 'react';
  import ReactDOM from 'react-dom';
  import './index.css';
  
  import reportWebVitals from './reportWebVitals';
  import {Marksheetfrom './Marksheet'
  
  ReactDOM.render(
    <React.StrictMode>
      <Marksheet />
    </React.StrictMode>,
    document.getElementById('root')
  );

  // If you want to start measuring performance in your app, pass a function
  // to log results (for example: reportWebVitals(console.log))
  // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
  reportWebVitals();



Marksheet Program using Uncontrolled Class Component:-

import React from "react";
export class Marksheetnew extends React.Component
{
 
    constructor(props)
    {
        super(props);
        this.text1 = React.createRef()
        this.text2 = React.createRef()
        this.text3 = React.createRef()
        this.text4 = React.createRef()
        this.text5 = React.createRef()
        this.sub1 = React.createRef()
        this.sub2 = React.createRef()
        this.sub3 = React.createRef()
        this.sub4 = React.createRef()
        this.sub5 = React.createRef()
       
        this.state={'res':''}
       

    }
   
    funMark=(event)=>
    {
        var arr=[];
       var sub=[];
         arr.push(parseInt(this.text1.current.value));
         arr.push(parseInt(this.text2.current.value));
         arr.push(parseInt(this.text3.current.value));
         arr.push(parseInt(this.text4.current.value));
         arr.push(parseInt(this.text5.current.value));
         sub.push(this.sub1.current.value);
         sub.push(this.sub2.current.value);
         sub.push(this.sub3.current.value);
         sub.push(this.sub4.current.value);
         sub.push(this.sub5.current.value);
       var c=0
       var total=0;
       var data='';
       var grace=0;
       var flag=true;
       var subjectname='';
       var distinctionname='';
       var per=0;
       for(var i=0;i<arr.length;i++)
       {
            if(arr[i]<0 || arr[i] > 100)
            {
                data = 'invalid marks';
                flag=false;
                break;
            }
            if(arr[i]<33)
            {
                c++;
                subjectname+=sub[i] + ' ';
                grace=arr[i];
            }
            if(arr[i]>=75)
            {
               
                distinctionname+=sub[i] + " ";
               
            }
            total+=arr[i];
       }
       if(flag)
       {
       if(c===0 || (c===1 && grace>=28))
       {
          per=total/5;
          if(c===0)
          {
           data = " Pass with  "+ per + "%";
         
          }
          else
          {
            data = " Pass by Grace and Grace subject is "+subjectname + " Grace mark is "+(33-grace);
            per=total+(33-grace)/5;
          }
          if(per>=33 && per<45)
          {
            data += " and  Division is Third ";
          }
          else if(per<60)
          {
            data += " and Division is Second ";
          }
          else
          {
            data += " and Division is First ";
          }
          if(distinctionname !== '')
          {
            data += ' Distinction Subject '+ distinctionname;
          }
       }
       else if(c===1)
       {
           data = "Suppl in  "+subjectname
       }
       else
       {
          data = "Fail in  "+subjectname
       }
    }
       this.setState({'res':`result is ${data}`})
       event.preventDefault()

    }
    render()
    {
    return(<div>
        <h1>Marksheet Program by Unctrolled Class Component</h1>
       <form onSubmit={this.funMark}>
       <input type="text" ref={this.sub1} placeholder="Enter First Subject Name" />
       
       <input type="text" ref={this.text1} placeholder="Enter First Number" />
       <br/>
       <input type="text" ref={this.sub2} placeholder="Enter Second Subject Name" />
       <input type="text" ref={this.text2} placeholder="Enter Second Number" />
       <br />
       <input type="text" ref={this.sub3} placeholder="Enter Third Subject Name" />
       <input type="text" ref={this.text3} placeholder="Enter Third Number" />
       <br />
       <input type="text" ref={this.sub4} placeholder="Enter Fourth Subject Name" />
       <input type="text" ref={this.text4} placeholder="Enter Fourth Number" />
       <br />
       <input type="text" ref={this.sub5} placeholder="Enter Fifth Subject Name" />
       <input type="text" ref={this.text5} placeholder="Enter Fifth Number" />
       <br />
       <input type="submit" value="Submit" />
       </form>
       {this.state.res}
       
    </div>)
    }
}


Marksheet Program using Uncontrolled Functional Component:-

import { useRef,useState } from "react"

function Marksheetdemo()
{
    const text1 = useRef()
    const text2 = useRef()
    const text3 = useRef()
    const text4 = useRef()
    const text5 = useRef()
    const sub1 = useRef()
    const sub2 = useRef()
    const sub3 = useRef()
    const sub4 = useRef()
    const sub5 = useRef()
    var arr=[];
    var sub=[];
    var[res,setRes] = useState(undefined)
    function funMark(event)
    {
       arr.push(parseInt(text1.current.value));
       arr.push(parseInt(text2.current.value));
       arr.push(parseInt(text3.current.value));
       arr.push(parseInt(text4.current.value));
       arr.push(parseInt(text5.current.value));
       sub.push(sub1.current.value);
       sub.push(sub2.current.value);
       sub.push(sub3.current.value);
       sub.push(sub4.current.value);
       sub.push(sub5.current.value);
       var c=0
       var total=0;
       var data='';
       var grace=0;
       var flag=true;
       var subjectname='';
       var distinctionname='';
       var per=0;
       for(var i=0;i<arr.length;i++)
       {
            if(arr[i]<0 || arr[i] > 100)
            {
                data = 'invalid marks';
                flag=false;
                break;
            }
            if(arr[i]<33)
            {
                c++;
                subjectname+=sub[i] + ' ';
                grace=arr[i];
            }
            if(arr[i]>=75)
            {
               
                distinctionname+=sub[i] + " ";
               
            }
            total+=arr[i];
       }
       if(flag)
       {
       if(c===0 || (c===1 && grace>=28))
       {
       
          if(c===0)
          {
           data = " Pass with  "+ per + "%";
           per=total/5;
          }
          else
          {
            data = " Pass by Grace and Grace subject is "+subjectname + " Grace mark is "+(33-grace);
            per=total+(33-grace)/5;
          }
          if(per>=33 && per<45)
          {
            data += " and  Division is Third ";
          }
          else if(per<60)
          {
            data += " and Division is Second ";
          }
          else
          {
            data += " and Division is First ";
          }
          if(distinctionname !== '')
          {
            data += ' Distinction Subject '+ distinctionname;
          }
       }
       else if(c===1)
       {
           data = "Suppl in  "+subjectname
       }
       else
       {
          data = "Fail in  "+subjectname
       }
    }
       setRes(`result is ${data}`)
       event.preventDefault()

    }
    return(<div>
       <form onSubmit={funMark}>
       <input type="text" ref={sub1} placeholder="Enter First Subject Name" />
       
       <input type="text" ref={text1} placeholder="Enter First Number" />
       <br/>
       <input type="text" ref={sub2} placeholder="Enter Second Subject Name" />
       <input type="text" ref={text2} placeholder="Enter Second Number" />
       <br />
       <input type="text" ref={sub3} placeholder="Enter Third Subject Name" />
       <input type="text" ref={text3} placeholder="Enter Third Number" />
       <br />
       <input type="text" ref={sub4} placeholder="Enter Fourth Subject Name" />
       <input type="text" ref={text4} placeholder="Enter Fourth Number" />
       <br />
       <input type="text" ref={sub5} placeholder="Enter Fifth Subject Name" />
       <input type="text" ref={text5} placeholder="Enter Fifth Number" />
       <br />
       <input type="submit" value="Submit" />
       </form>
       {res}

    </div>)
}

export default Marksheetdemo;

Marksheet program using Object type under uncontrolled functional component?

import { useRef,useState } from "react"

function Marksheetdemo()
{
    const text1 = useRef()
    const text2 = useRef()
    const text3 = useRef()
    const text4 = useRef()
    const text5 = useRef()
    const sub1 = useRef()
    const sub2 = useRef()
    const sub3 = useRef()
    const sub4 = useRef()
    const sub5 = useRef()
    var arr={};
    var[res,setRes] = useState(undefined)
    function funMark(event)
    {
   
       
       arr[sub1.current.value]=parseInt(text1.current.value);
       arr[sub2.current.value]=parseInt(text2.current.value);
       arr[sub3.current.value]=parseInt(text3.current.value);
       arr[sub4.current.value]=parseInt(text4.current.value);
       arr[sub5.current.value]=parseInt(text5.current.value);
     
       var c=0
       var total=0;
       var data='';
       var grace=0;
       var flag=true;
       var subjectname='';
       var distinctionname='';
       var per=0;
       for(var key in arr)
       {
            var m = arr[key];
            if(m<0 || m > 100)
            {
                data = 'invalid marks';
                flag=false;
                break;
            }
            if(m<33)
            {
                c++;
                subjectname+=key + ' ';
                grace=m;
            }
            if(m>=75)
            {
               
                distinctionname+=key + " ";
               
            }
            total+=m;
       }
       if(flag)
       {
       if(c===0 || (c===1 && grace>=28))
       {
       
          if(c===0)
          {
            per=total/5;
           data = " Pass with  "+ per + "%";
         
          }
          else
          {
            per=total+(33-grace)/5;
            data = " Pass by Grace and Grace subject is "+subjectname + " Grace mark is "+(33-grace);
           
          }
          if(per>=33 && per<45)
          {
            data += " and  Division is Third ";
          }
          else if(per<60)
          {
            data += " and Division is Second ";
          }
          else
          {
            data += " and Division is First ";
          }
          if(distinctionname !== '')
          {
            data += ' Distinction Subject '+ distinctionname;
          }
       }
       else if(c===1)
       {
           data = "Suppl in  "+subjectname
       }
       else
       {
          data = "Fail in  "+subjectname
       }
    }
       setRes(`result is ${data}`)
       event.preventDefault()

    }
    return(<div>
       <form onSubmit={funMark}>
       <input type="text" ref={sub1} placeholder="Enter First Subject Name" />
       
       <input type="text" ref={text1} placeholder="Enter First Number" />
       <br/>
       <input type="text" ref={sub2} placeholder="Enter Second Subject Name" />
       <input type="text" ref={text2} placeholder="Enter Second Number" />
       <br />
       <input type="text" ref={sub3} placeholder="Enter Third Subject Name" />
       <input type="text" ref={text3} placeholder="Enter Third Number" />
       <br />
       <input type="text" ref={sub4} placeholder="Enter Fourth Subject Name" />
       <input type="text" ref={text4} placeholder="Enter Fourth Number" />
       <br />
       <input type="text" ref={sub5} placeholder="Enter Fifth Subject Name" />
       <input type="text" ref={text5} placeholder="Enter Fifth Number" />
       <br />
       <input type="submit" value="Submit" />
       </form>
       {res}

    </div>)
}

export default Marksheetdemo;

Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...