Project Help || Divide by zero JS

Hi,

I’m learning Javascript and for a small assignment I’m struggling to figure something out.

I have an exam result calculator that I need to create.

I always have to divide the the biggest number with the smallest. I can’t have decimals.
I’m not allowed to enter zero’s as divider. ex. 5 / 0 is not allowed but 0 / 5 = 0 is allowed.

With this last one I’m struggling how to add this to the working code I have now.
Any guidance would be helpful?

<script>
var eKnop = document.querySelector('#deKnop');
eKnop.onclick = bereken;

function bereken() {


var eGetal1 = document.getElementById('getal1');
var eGetal2 = document.getElementById('getal2');

// de getallen

var nGetal1 = parseInt(eGetal1.value);
var nGetal2 = parseInt(eGetal2.value);



var sResultaat = "";

if (nGetal2 == 0 || nGetal1 == 0){
    sResultaat += " 0 not allowed";

} else if (nGetal1 > nGetal2) { 
    sResultaat = nGetal1 / nGetal2;

} else { 
    sResultaat = nGetal2 / nGetal1;
}

//console.log(sResultaat);
var eOutput = document.querySelector('#output');
eOutput.innerHTML = sResultaat;
}

</script>

Try:

const [x, y] = [eGetal1.value, eGetal2.value].map(n => parseInt(n));
const result = y !== 0 ? x / y : 'Error';

But this method would skip the if else statement? correct?

The assignment requires this.

Conditional (aka ternary) operator is de facto if ... else statement:

When I try your solution then 9 / 9 = 9.

The zero part works.

I’m correct to say that the .map places the highest number first in the equation?

Sorry I’m very very new at this

Yes, it’s saying x and y are respectively (so the order matters here) equal to taking the value of eGetal1 and eGetal2 (in that order) and applying parseInt to each one. Put the two eGetals in an array, apply parseInt to both, and you get an array of two numbers. So [x, y] is saying x is the first number, y is the second.

const [x, y] = [eGetal1.value, eGetal2.value].map(n => parseInt(n));

Is basically equivalent to:

const x = parseInt(eGetal1.value);
const y = parseInt(eGetal2.value);

@DanCouper @snigo
What happens with the (n) part? Sorry for all the question hard to get my head around and still make sure I get the assignment correct.

I think I almost have it though, only thing is that I get infinity when trying 3/0

const [nGetal1,nGetal2] = [eGetal1.value, eGetal2.value].map(n => parseInt(n));
const tResult = nGetal2 != 0 ? nGetal1 : "not allowed"

var tResultaat = "";

if (nGetal1 > nGetal2 !=0 ? nGetal1: "not allowed") { 
    sResultaat = nGetal1 / nGetal2 ;

} else { 
    sResultaat = nGetal2 / nGetal1;
}

//console.log(sResultaat);
var eOutput = document.querySelector('#output');
eOutput.innerHTML = sResultaat;
console.log = sResultaat; 
}

This:

if (nGetal1 > nGetal2 !=0 ? nGetal1: "not allowed") { 

Is not valid. I kinda feel like you’re trying to absorb far too many concepts here for what should be a very simple challenge.

On that line, n is the name of the parameter for the function you give to map. It could be z or flurm, it’s just a function parameter. Map runs that function once for every value in the original value. I need to emphasise here, that line is the same as this:

const x = parseInt(eGetal1.value);
const y = parseInt(eGetal2.value);

If you are having difficulty with the syntax of an if statement, wrestling with how destructing the result of mapping an array works is not really helpful here: what that line of pretty complex code does is, as I say, exactly the same as the above two lines, which are the same as what you originally had

@DanCouper
I have that feeling too… about the concepts.
I’m already further in other assignment except for my second assignment which is the one above.
I’m unable to grasp the one part where I can’t divide x / 0 but I can divdide 0 / x = 0.

Breaking my head over this an all documentation are not helping me actually…

You can’t divide by 0 because it doesn’t make sense. Not from a JS perspective, but from a maths and logical perspective, it’s not possible. With division you’re basically repeatedly substracting and counting how many times you can subtract:

4 / 2
4 - 2 - 2 to get to zero
That’s 2
4 / 2 is 2

4 / 0
4 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 …
That’s infinity
4 / 0 is infinity, it’s impossible

0 / 4
0 is already 0, don’t need to subtract anything
0 / 4 is 0

1 Like

I told my coach that, but he said that it’s possible to divide 4 / 0 but not 5 / 0 because 0 is not a divider.
I also addressed that to him that I found that weird.

Anyways I’ll try some more till I get it.

@DanCouper

You’re either misunderstanding something they are saying or they have some bizarre ideas about arithmetic; you can’t divide by zero using normal arithmetic, that operation is meaningless. You can’t divide 4 by 0. Or 5, or 6, or 7.54332, or 10000, doesn’t make any difference. This isn’t a JS thing, division by zero just makes absolutely no sense at all.

In JS, division by zero results in infinity. This isn’t strictly true (it’s actually undefined as it’s meaningless) but it’ll do. You just need the check if y is 0: if it is, print the string saying it’s not allowed, otherwise do the division.