score:1

Accepted answer

that's simply not how startswith works. you can only supply it a single test string (and, optionally and rarely, a start position.)

try replacing

! prod.productid .startswith ('bgm', 's11', 'bgp')

with

! (['bgm', 's11', 'bgp'] .some (s => prod .productid .startswith (s)))

or equivalently,

(['bgm', 's11', 'bgp'] .every (s => ! prod .productid .startswith (s)))

depending on which makes more sense to you, and doing the same for your currentprice. that may capture what you need.

score:1

string.prototype.startswith, cannot be passed three separate strings to check.

if your pseudo code is:

if product id does not begin with bgm, s11, or bgp,

then you could test it with a regular expression or else use three separate statements like so:

let discountprice = (!prod.productid.startswith('bgm') &&
    !prod.productid.startswith('s11') &&
    !prod.productid.startswith('bgp') &&
    prod.currentprice.tostring().endswith('97'))
    ? parsefloat(prod.currentprice) * 0.05
    : !prod.productid.startswith('bgm', 's11', 'bgp')
        ? parsefloat(prod.currentprice) * 0.15
        : 0

using a regex:

let discountprice = (!/^(s11|bgp|bgm)/.test(prod.productid) && prod.currentprice.tostring().endswith('97'))
    ? parsefloat(prod.currentprice) * 0.05
    : !prod.productid.startswith('bgm', 's11', 'bgp')
        ? parsefloat(prod.currentprice) * 0.15
        : 0

score:2

it is not possible to pass multiple options into startswith() method.

the method has following syntax: string.startswith(searchvalue, start)

you can try:

  1. checking startswith for each string separately:
!(prod.productid.startswith('bgm') || prod.productid.startswith('s11') || prod.productid.startswith('bgp'))
  1. looping through array of options:
!['bgm', 's11', 'bgp'].some(item => prod.productid.startswith(item))
  1. using regular expression:
!/^(bgm|s11|bgp)/.test(prod.productid)

Related Query

More Query from same tag