1

I'm writing a simple discount app with prompt input. The problem is in my ternary operator - it doesn't count the newPrice as well as show that input is invalid. Can you tell me what I'm doing wrong?

I if the input is wrong I want to assign newPrice to null and alert 'Invalid data'

My code:

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

const newPrice = ((9999999 > price > 0) && (99 > discount > 0)) ? (price - price * discount / 100) : (null, alert('Invalid data'));
console.log(newPrice)

3
  • probably an operator precedence thing. when in doubt, put more parenthesis around things Commented Feb 5, 2019 at 6:58
  • Works fine for me in Chrome and Firefox Dev console Commented Feb 5, 2019 at 7:03
  • please check your conditions and if you are having lot of decision making then why can't you go with if-else conditions Commented Feb 5, 2019 at 7:19

4 Answers 4

1

Try separating out the code statements to

  1. make sure new price is actually null. Not undefined. (per your question)
  2. the a > b > c stuff does not work afaik in JS. Please refactor that syntax as i did below or similar.

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

const newPrice = (price && (9999999 > price) && discount && (99 > discount)) ? (price - price * discount / 100) : null;
console.log(newPrice);
if(!newPrice){alert('Invalid data');}

Sign up to request clarification or add additional context in comments.

Comments

0

First, convert your inputs to numbers, and second, refactor your conditions to be valid (you can't have x > y > z IIRC):

const price = parseInt(prompt('Please, enter the price:'));
const discount = parseInt(prompt('Please, enter the discount amount:'));

const newPrice = (((9999999 > price) && price) && ((99 > discount) && discount)) ? (price - price * discount / 100) : (null, alert('Invalid data'));
console.log(newPrice);

Comments

0

The prompt may be reading the value as a string. Convert it to number using parseInt or unary +

const price = parseInt(prompt('Please, enter the price:'));
const discount = parseInt(prompt('Please, enter the discount amount:'));

const newPrice = ((price && price > 0 && price < 9999999) && (discount && discount >0 && discount < 99 )) ? (price - price * discount / 100) : (null, alert('Invalid data'));
console.log(newPrice)

10 Comments

string and number comparison works fine when the string only contains numbers
@ChrisRollins It doesn't, try console.log('10' > '9');.
moot. good question though. the input prompt is actually taking the number as a number (you can run the code snippet here in SO) and doing the substraction math correctly.
so it's not necessary to add + or parseInt before prompt?
@ChrisRollins Yeah, but in general your code should not rely on automatic type coercion.
|
0

Here is the example of doing by both method if-else and ternary operator (?:). But I would recommend going with if-else if you have much decision makings.

var price = prompt('Please, enter the price:');
var discount = prompt('Please, enter the discount amount:');
var newPrice;
if((price>0 && price < 9999999)&&(discount>0 && discount < 99)){
newPrice=price - (price * discount) / 100;
}else{
newPrice='null';
alert('Invalid data');
}
var newPrice1 = ((9999999 > price && price > 0) && (99 > discount && discount > 0)) ? (price - (price * discount) / 100) : ('null', alert('Invalid data'));
console.log(newPrice);
console.log(newPrice1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.