Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3356432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:30:47+00:00 2026-05-18T02:30:47+00:00

Hi I am newbie in programming and I am trying to learn and make

  • 0

Hi I am newbie in programming and I am trying to learn and make it work with each(). Bear with me. I try my best to learn from here and you.

  1. I am trying to go through the item per product in a catalog for the specific prices: either original and sale from the page.
  2. then calculate the % for discount
  3. print discount %
  4. check to compare the percent to color the background: brown, yellow and red.

Now, I test each line to see if it works or not.
salecost = $(this).find('#sale').html(); returns a few nulls before displays amount with dollar signs. Weird couldn’t figure that one out. Replace() isn’t working right – couldn’t get it working. It is supposed to remove dollar sign.

Also, I am not sure how it goes with compare — do i write statement correctly?

Thank you in advance for the help

var salecost;
var originalcost;
var percentDiscount;
var percent; 

function calculate(sale, original)
 {
  percentDiscount = eval((sale/original)*100);
        document.getElementById("percentoff").innerHTML=parseInt(percentDiscount) + '%';       
 }

$(document).ready(function(index){

           $('.item').each(function(){     
            salecost = $(this).find('#sale').html();
            salecost = salecost.replace(/[^\d\.]/g,"");
            alert (salecost);
             originalcost = $('#sale').html();
             originalcost = originalcost.replace(/[^\d\.]/g,"");
             alert (originalcost);
             percent = calculate(salecost,originalcost);
             alert(percent);

               if(percent<30)
               {
                       $("div#percentoff").css({"background-color":"brown", "padding":"5px 0"});
               }

               if(percent<50){
                       $("div#percentoff").css({"background-color":"yellow", "padding":"5px 0"});
               }

               if(percent<70){
                       $("div#percentoff").css({"background-color":"red", "padding":"5px 0"});
               }
       });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:30:48+00:00Added an answer on May 18, 2026 at 2:30 am
    1. You should move your var statements to within the each function so they’re not global.
    2. You don’t need to you the eval, (sale/original)*100 will work by it self.
    3. It’s better to use consistent style, your calculate function could be written using jquery.

      $(“#percentoff”).html(percentDiscount) + ‘%’);

    4. casting is unnecessary for most cases in javascript, concatenating a number with a string will produce a string.

    5. your calculate function should return percentDiscount;
    6. Instead of using .css() it would be better to use .addClass that way it’s easy to undo with .removeClass, the style is all the the style sheet and you can use jquery to select the elements with the class.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie programmer doing his best to self learn PHP. I'm trying to
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
I am newbie to iphone programming and trying to learn iphone programming.I need experts
I'm a newbie in iPhone Programming. I'm trying to send a message from one
I'm a Perl newbie. I'm trying to learn based on a previous person's work
I am a Java newbie trying to learn network programming and concurrency, and I
I'm a struggling newbie to programming and trying to make a freemium style app
I'm a newbie into Android programming... Here's what I'm trying to do for a
I'm a socket programming newbie. Here's a snippet: struct sockaddr_storage client_addr; ... client_addr_size =
I'm a erlang newbie. When I read the Socket chapter from Programming Erlang and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.