Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not taking value from name input box and not formatting string

I want my program to take the name value that the user has entered and add it to the string.When the user presses the submit button.I would like to know why its not letting the user enter this.

My program works by leting the user enter the grade that they have got and responding with a string that tell then how well the did.

var name1 = document.getElementById('firstName').value;
var name2 = document.getElementById('secondName').value;

const physic = document.getElementById('physic');
const chemistry = document.getElementById('chemistry');
const biology = document.getElementById('biology');
const mathematics = document.getElementById('mathematics');
const computer = document.getElementById('computer');

var num1,num2,num3,num4,num5;

let store = document.getElementById('store');

function markChecker() {

   num1 = parseInt(physic.value);
   num2 = parseInt(chemistry.value);
   num3 = parseInt(biology.value);
   num4 = parseInt(mathematics.value);
   num5 = parseInt(computer.value);

  

  grader(num1,'physics')
  grader(num2,'chemistry')
  grader(num3,'biology')
  grader(num4,'mathamatics')
  grader(num5,'computer science')
}


function grader(grade, subject) {
element = document.createElement('div')

  if (grade >= 90) {
    element.textContent = name1+' got Grade A in ' + subject + ' well done';
    store.appendChild(element)
  } else if (grade >= 80) {
    element.textContent = name1+'You got Grade B in ' + subject + ' great mark';
    store.appendChild(element)
  } else if (grade >= 70) {
    element.textContent = name1+'You got Grade C in ' + subject + ' got a good mark';
    store.appendChild(element)
  } else if (grade >= 60) {
    element.textContent = name1+'You got Grade D in ' + subject ;
    store.appendChild(element)
  } else if (grade >= 40) {
    element.textContent = name1+'You got a Grade E in ' + subject + ' try harder next time';
    store.appendChild(element)
  } else if (grade < 40) {
    element.textContent = name1+'You got Grade F in ' + subject + ' get better or you will be a loser';
    store.appendChild(element)
  } 
 }     
    <h1>Enter the mark for the student</h1>
        <input id="firstName"  placeholder="Enter first name" type="text">
       
        <input id="secondName"  placeholder="Enter last name" type="text">

    <div>        
        <p>Physics</p>
        <input id="physic"  placeholder="Enter your mark out of 100" type="number">
    </div>   
   
    <div>
        <p>Chemistry</p>
        <input id="chemistry"  placeholder="Enter your mark out of 100" type="number">
    </div>
   
    <div>   
        <p>Biology</p>
        <input id="biology"  placeholder="Enter your mark out of 100" type="number">
    </div>
   
    <div>
        <p>Mathematics </p>
        <input id="mathematics"  placeholder="Enter your mark out of 100" type="number">
    </div>
   
    <div>
        <p>Computer science</p>
        <input id="computer"  placeholder="Enter your mark out of 100" type="number">
    </div>
   
    <div>
   <button onclick="markChecker()">Submit</button>
    </div>
    
        <div id="store"></div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to grab the current value of the firstName field inside grader().

const
  firstName = document.getElementById('firstName'),
  lastName = document.getElementById('secondName'),
  physic = document.getElementById('physic'),
  chemistry = document.getElementById('chemistry'),
  biology = document.getElementById('biology'),
  mathematics = document.getElementById('mathematics'),
  computer = document.getElementById('computer'),
  store = document.getElementById('store');

function markChecker() {
  const
    num1 = parseInt(physic.value),
    num2 = parseInt(chemistry.value),
    num3 = parseInt(biology.value),
    num4 = parseInt(mathematics.value),
    num5 = parseInt(computer.value);

  grader(num1, 'physics')
  grader(num2, 'chemistry')
  grader(num3, 'biology')
  grader(num4, 'mathamatics')
  grader(num5, 'computer science')
}


function grader(grade, subject) {
  const element = document.createElement('div');

  if (grade >= 90) {
    element.textContent = `${firstName.value}, got an A in ${subject}. Well done.`;
  } else if (grade >= 80) {
    element.textContent = `${firstName.value}, got an B in ${subject}. Great mark.`;
  } else if (grade >= 70) {
    element.textContent = `${firstName.value}, got an C in ${subject}. Good mark.`;
  } else if (grade >= 60) {
    element.textContent = `${firstName.value}, got an D in ${subject}.`;
  } else if (grade >= 40) {
    element.textContent = `${firstName.value}, got an E in ${subject}. Try harder next time.`;
  } else if (grade < 40) {
    element.textContent = `${firstName.value}, got an F in ${subject}. You are flunking-out!`;
  }
  store.appendChild(element);
}
<h1>Enter the mark for the student</h1>
<input id="firstName" placeholder="Enter first name" type="text">
<input id="secondName" placeholder="Enter last name" type="text">
<div>
  <p>Physics</p>
  <input id="physic" placeholder="Enter your mark out of 100" type="number">
</div>
<div>
  <p>Chemistry</p>
  <input id="chemistry" placeholder="Enter your mark out of 100" type="number">
</div>
<div>
  <p>Biology</p>
  <input id="biology" placeholder="Enter your mark out of 100" type="number">
</div>
<div>
  <p>Mathematics </p>
  <input id="mathematics" placeholder="Enter your mark out of 100" type="number">
</div>

<div>
  <p>Computer science</p>
  <input id="computer" placeholder="Enter your mark out of 100" type="number">
</div>
<div>
  <button onclick="markChecker()">Submit</button>
</div>
<div id="store"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading