Replace is not a Function

Asked

Viewed 10,837 times

4

I created a script to insert a div in my store cart, but it is giving error in the replace function. Would anyone know why ?

var $JQuery = jQuery.noConflict();
$JQuery(function(){ 
var ValorFreteGratis = 299;
var PrecoTotal = '{{order_total}}';
var QuantoFalta = ValorFreteGratis - PrecoTotal;
var NumeroComVirgula = QuantoFalta.replace(".", ",");
if(PrecoTotal < ValorFreteGratis){
$JQuery('<div class="msg-free_shipping"> <p><span class="warning">EI!!:</span> Comprando mais R$' + NumeroComVirgula.toFixed(2) + ' <span class="restante">você aproveita o <strong>FRETE GRÁTIS</strong> nas compras acima de R$<span class="valor-gratis">' + ValorFreteGratis + '</span></p> </div>').insertAfter('form#cart_update_post');}});

Keeps giving the error: "Uncaught Typeerror: Quantofalta.replace is not a Function"

  • Probably QuantoFalta it’s not a string, maybe undefined or NaN. See this way: console.log(typeof QuantoFalta) before the row applying the replace.

  • How high is number by account, it is necessary to convert to string

  • 1

    If Quantofalta is not string you can do that: String(QuantoFalta).replace...

3 answers

11


The function replace is a function of the type String. The variable QuantoFalta is the product of a mathematical operation, so it is not a String. Utilize toString to transform it:

 ...
 ... QuantoFalta.toString().replace(...
 ...

String.prototype.replace()

The method replace() returns a new string with some or all combinations of the pattern replaced by a replacement.

4

Your problem is with typing (you can see by TypeError), as you are making a subtraction, the whole result becomes the type number, hence, the function replace there is no.

var x = 10 - 5; // deve dar 5
console.log(x) // 5
console.log(typeof x) // number
x.replace(5, 10) // Uncaught TypeError

What can be done is to change the type of the variable, using String(valor):

    var x = 10 - 5; // deve dar 5
    console.log(x) // 5
    
    x = String(x).replace('5', 'agora foi') // note o uso do String(x)
    console.log(x) // "agora foi"
    
    // O string pode ser feito na conta também
    // Ex:
    var y = String(10 - 5)
    console.log(y)
    

Usually these errors cause confusion because Javascript is not a well-typed language, it is not necessary to make this explicit during use. And another thing that can also cause problems is, you can substitute/multiply/divide strings and they will become number, but cannot add them, since it is the same character used to concatenate, this something else that could create this kind of confusion.

console.log(typeof ('10' - '5')) // number (5)
console.log(typeof (10 + 5)) // number (15)
console.log(typeof ('10' + 5)) // string ('105') 
console.log(typeof (10 + '5')) // string ('105')
console.log(typeof ('10' + '5')) // string ('105')

1

replace can only be given with String. Change to:

var numeroComVirgula = String(QuantoFalta).replace('.',',')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.