What could stop a FOR? (javascript)

Asked

Viewed 111 times

-1

I am having a problem in a script, the same stops in a FOR and does not execute the rest of i.

In the case it is on that line: for (i = 1; i < ultimoId;i++) {

line 43 if you are looking at any Ide.

Follows the script:

$(document).ready(function () {


    var conf = [{
        codigo: "",
        desconto: "",
        string: "",
        auxString: "",
        auxNum: "",
        descontoNum: "",
        auxDesconto: "",
        valorFinal: "",
        auxFinal2: "",
        valorFinalDesconto: "",
        idInicial: ""
    }];

    function apenasNumeros(string) {
        var numsStr2 = string.replace(/[^0-9]/g, '');
        return parseInt(numsStr2);
    };

    function apenasNumeros2(desconto) {
        var InternoDesconto = desconto.replace(/[^0-9]/g, '');
        return parseInt(InternoDesconto);
    };

    var i = "";
    var j = "";
    var string = "";
    var auxString = "";
    var auxNum = "";
    var desconto = "";
    var descontoNum = "";
    var auxDesconto = "";
    var valorFinalDesconto = "";
    var valorFinal = "";
    var auxFinal2 = "";
    var resultado = "";

    var ultimoId = $(".shelfNewPriceWrapper").attr("id");
    j = 0;
    for (i = 1; i < ultimoId;i++) {

        string = $("#" + i + "").html();
        auxString = apenasNumeros(string);

        auxNum = auxString / 100;

        desconto = $(".highlightWrapper p").html();

        descontoNum = apenasNumeros2(desconto);

        auxDesconto = descontoNum / 100;

        valorFinalDesconto = (auxNum * auxDesconto);
        valorFinal = (auxNum) - (valorFinalDesconto);
        auxFinal2 = valorFinal.toFixed(2);

        conf[j].codigo = i;
        conf[j].desconto = desconto;
        conf[j].string = string;
        conf[j].auxString = auxString;
        conf[j].auxNum = auxNum;
        conf[j].descontoNum = descontoNum;
        conf[j].auxDesconto = auxDesconto;
        conf[j].valorFinal = valorFinal;
        conf[j].auxFinal2 = auxFinal2;
        conf[j].valorFinalDesconto = valorFinalDesconto;

        resultado = conf[j].auxFinal2;
        console.log("indice:" + j);
       
        $("#" + i + "").html('R$ ' + resultado + ' ');
        j++;
        

        string = "";
        auxString = "";
        auxNum = "";
        desconto = "";
        descontoNum = "";
        auxDesconto = "";
        valorFinalDesconto = "";
        valorFinal = "";
        auxFinal2 = "";
        resultado = "";

    };
        

});

What could be going on so he doesn’t perform that FOR ?

FOLLOW THE HTML CODE - DETAIL WE USE VTEX

#set($id = $product.Id)
#set($uri = $product.Uri)
#set($escapedName = $product.HtmlEscapedName)
#set($evaluationRate = $product.EvaluationRate) 

<!-- class: shelf prateleira vitrine home -->
<div class="shelfImageWrapper">
	<div class="shelfButtonWrapper">
		<div class="shelfAmountInCart">
			$product.AmountInCart
		</div>
		<!--<div class="shelfBuyButtomWrapper">
			$product.BottomBuyAsynchronous
		</div>-->
	</div>
	<a class="shefImage" title="$escapedName" rel="nofollow" href="$uri">
		<div class="shelfImage-1">$product.GetImageTag(235,235)</div>
	
	</a>
</div>

<div class="shelfInformationWrapper">
	<input type="hidden" value="$product.BestPrice" class="qd_productPrice" />
	<input type="hidden" value="$product.ListPrice" class="qd_productOldPrice" />
	<input type="hidden" value="$product.NumbersOfInstallment" class="qd_sp_installments" />
	<h3><a title="$escapedName" href="$uri" rel="nofollow">$product.Name</a></h3>
	#if ($product.IsInStock)
		<div class="yv-review-quickreview" value="$id"></div>
		<p class="shelfPriceWrapper">
			<a title="$escapedName" rel="nofollow" href="$uri">
				#if ($product.HasBestPrice)
					<span class="shelfOldPriceWrapper">De $product.ListPrice</span>
					<br/>
				#end
			Por	<span class="shelfNewPriceWrapper" id="$id">$product.BestPrice </span> <span>à vista</span>
				<br/>
				<span class="installmentWrapper">
					#if ($product.NumbersOfInstallment > 1)
						em até
						<strong class="installment"> ${product.NumbersOfInstallment}x </strong> 
						de
						<strong class="InstallmentValue"> $product.InstallmentValue </strong> 
						<span>s/juros</span>
					#end
				</span>
					#if ($product.HasBestPrice)
						<span class="saveAmount">economize <span class="qd_saveAmount">R$ </span> <small>(</small><span class="qd_saveAmountPercent">%</span><small>)</small></span>
					#end
			</a>
			 $product.BottomBuyAsynchronous
		</p>
	#else
		<p class="outOfStock">Produto Indisponível</p> 
	#end
</div>
#if ($product.IsInStock)
	<div class="shelfStampsWrapper">
		<a title="Clique para ver as condições de frete" class="highlightWrapper" rel="nofollow" href="#" data-reveal-id="frete-gratis-vitrine" data-animation="fade">$product.HightLight</a>
		<a title="$escapedName" class="highlightWrapper" rel="nofollow" href="$uri">$product.DiscountHightLight</a>
	</div>
#end

  • 1

    What the ID of the field containing that class shelfNewPriceWrapper ?

  • 1

    I think the problem may be that ultimoId be a string. Forehead like this: ultimoId = parseInt(ultimoId, 10); in the line before for (i = 1; i < ultimoId;i++) {

  • 1

    because i start with 1 ? the correct would be with 0

  • 1

    According to the attr documentation() he "returns the attribute value of the first element selected". So if you have more than one element with the class .shelfNewPriceWrapper may not have the id you want.

  • The shelfNewPriceWrapper ID I made other script to generate the ID in the case is a different ID for cado one that generates

  • 1

    Convert the last variableId to int that should work.

  • @Thadeu converted to comment your response for being too short and better fit as a comment. I also think that’s the case as I commented above. Let’s see what Julio says...

  • you’d better declare yours so: for (var i = 1; i < ultimoId;i++) {...} doesn’t make much sense either: $("#" + i + "").html(.. ... can do just that: $('#' + i).html(...

  • here too ...).html('R$ ' + resultado);

  • Publish your HTML as you want it to be output, because your code besides done badly is very confusing.

  • I updated the question with the HTML post, I hope it helps.

  • @Did you manage to solve this problem? if not join please the rendered HTML and not only what is on the server.

  • Hello Sergio managed to solve this problem otherwise, I will be posting the script.

Show 8 more comments

1 answer

1

I was able to solve the problem of my code as follows:

"undefined" != typeof jQuery && jQuery(".flag").each(function() {
  var e = jQuery(this);
  if (e.is(':contains(",")') || e.is(':contains(".")')) {
    var t = e.html();
    t = t.split(" - ");
    var r, i = t[0];
    i = jQuery.trim(i), "%" == i.substr(i.length - 1, 1) ? (i = i.substr(0, i.length - 1), r = "%") : (i = i.substr(2), r = "R$"), i = jQuery.trim(i), i = i.replace(",", "."), i = Number(i);
    var a = e.parents(".data").find(".bestPrice").html();
    if (a = jQuery.trim(a), a = a.substr(3), a = a.replace(",", "."), a = Number(a), "%" == r) {
      var s = a * i / 100,
        n = Math.ceil(a - s);
      n = n.toFixed(2)
    } else if ("R$" == r) {
      var n = Math.ceil(a - i);
      n = n.toFixed(2)
    }
    e.parents(".data").find(".bestPrice").html("R$" + n), e.html(t[1])
  }
});

Browser other questions tagged

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