0

I want the program to increase the number of notes when the user enters a number higher than 5 if the user enters 12 then its should say that you have 2 notes. I would also my to know if my code could be made more readable and more efficient

const box = document.getElementById('box');
const store = document.getElementById('store');

function notesChecker() {
  let num = parseInt(box.value);

  if (num / 5 == 0) {
    store.textContent = 'You go this number of notes ' + num;
  } else {
    store.textContent = 'You dont have enough money for notes';
  }
}
body {
  text-align: center;
}
<h1>Number of notes</h1>
<p>Enter a number and it will tell you the number of notes you will get in £5 notes.</p>
<input id="box" type="number">
<button onclick="notesChecker()">Submit</button>
<div id="store"></div>
  • 1
    The `/` operator is not an integer division operator. So `num / 5 == 0` will not be true when `num` is 1, 2, 3, or 4. You speak of *increase*, but there is nothing in your code that does that. – trincot Aug 25 '22 at 10:13
  • 1
    as currently written, your code outputs `You dont have enough money for notes` unless you input `0` (in which case it will output `You go this number of notes 0`) – GrafiCode Aug 25 '22 at 10:16
  • you supposedly need to determine the number of notes based on the input... it's as easy as taking the input number, dividing by 6 and storing that number in a variable that you will show in the output message after checking if that value is higher than zero – Diego D Aug 25 '22 at 10:17
  • 1
    Something like this: https://jsfiddle.net/hbsu6w8j/ –  Aug 25 '22 at 10:17
  • 1
    Although the function is called `parseInt()` all numbers in JavaScript are floats. – Andreas Aug 25 '22 at 10:17
  • 1
    Duplicate [How to perform an integer division, and separately get the remainder, in JavaScript?](https://stackoverflow.com/questions/4228356/how-to-perform-an-integer-division-and-separately-get-the-remainder-in-javascr) –  Aug 25 '22 at 10:17

2 Answers2

2

It looks like you expected the / operator to perform an integer division, but that is not the case. For example 3 / 5 is 0.6, not 0.

Secondly, you don't want to display the input number in the output message, but the number of notes, for which you need to use the integer division by 5.

Finally, if that quotient is 0, then you should display the other message ("you don't have enough..."), so you need to either swap the messages or invert the if condition.

You can use Math.floor to achieve what you want:

const box = document.getElementById('box');
const store = document.getElementById('store');

function notesChecker() {
  let num = parseInt(box.value);
  let notes = Math.floor(num / 5);
  if (notes > 0) {
    store.textContent = "You got this number of notes " + notes;
  } else {
    store.textContent = "You don't have enough money for notes";
  }
}
body {
  text-align: center;
}
<h1>Number of notes</h1>
<p>Enter a number and it will tell you the number of notes you will get in £5 notes.</p>
<input id="box" type="number">
<button onclick="notesChecker()">Submit</button>
<div id="store"></div>
trincot
  • 317,000
  • 35
  • 244
  • 286
1
const box = document.getElementById('box');
const store = document.getElementById('store');

function notesChecker(){

  let num = parseInt(box.value);

   if(num<5){
    store.textContent ='You dont have enough money for notes'; 
   }else{
    num=parseInt(num / 5);
    store.textContent='You get this number of notes '+ num ;
  }

}
  • 2
    What's the problem in OPs script? What have you changed? Why did you change it? And why do you call `parseInt()` on a number? – Andreas Aug 25 '22 at 10:17