skip to Main Content

I’m new to JavaScript. Now I’m trying to write code that displays hearts based on numbers. Everything came out looking good. But there are some that are displayed incorrectly, namely the number 500. It is displayed as ๐Ÿ’๐Ÿงก But 600 it is displayed as ๐Ÿ’ And it happened to 5000 and 6000 too, I can’t find a solution right now.

The Code:

function numbertoheart(r) {
  if (r <= 0) return "";
  for (var o = "", f = 0; f < Math.floor(r / 100000); f++) o += "๐Ÿ’Ÿ";
  for (var $ = r % 100000, f = 0; f < Math.floor($ / 50000); f++) o += "๐Ÿ’–";
  for (var $ = r % 50000, f = 0; f < Math.floor($ / 10000); f++) o += "๐Ÿ’—";
  for (var $ = r % 10000, f = 0; f < Math.floor($ / 5000); f++) o += "๐Ÿ’“";
  for (var $ = r % 5000, f = 0; f < Math.floor($ / 2000); f++) o += "โฃ";
  for (var $ = r % 2000, f = 0; f < Math.floor($ / 1000); f++) o += "๐Ÿ’•";
  for (var $ = r % 1000, f = 0; f < Math.floor($ / 500); f++) o += "๐Ÿ’";
  for (var $ = r % 500, f = 0; f < Math.floor($ / 200); f++) o += "๐Ÿ’š";
  for (var $ = r % 200, f = 0; f < Math.floor($ / 100); f++) o += "๐Ÿงก";
  for (var $ = r % 100, f = 0; f < Math.floor($ / 50); f++) o += "๐Ÿ’œ";
  for (var $ = r % 50, f = 0; f < Math.floor($ / 10); f++) o += "๐Ÿ’™";
  for (var $ = r % 10, f = 0; f < Math.floor($ / 5); f++) o += "๐Ÿ’›";
  for (var $ = r % 5, f = 0; f < $; f++) o += "โค";
  return o
}

console.log(numbertoheart(500));
console.log(numbertoheart(600));
console.log(numbertoheart(5000));
console.log(numbertoheart(6000));

Thank you in advance.

I want it to display correctly. It is similar to the rank system based on the number of points.

2

Answers


  1. The issue with your code is that it processes the input number in multiple cases when it matches multiple conditions. To fix this, you should subtract the hearts added in each case from the original number to make sure it’s only processed once.

    Fixed Code:

    function numbertoheart(r) {
      if (r <= 0) return "";
      var o = "";
      o += "๐Ÿ’Ÿ".repeat(Math.floor(r / 100000));
      r %= 100000;
      o += "๐Ÿ’–".repeat(Math.floor(r / 50000));
      r %= 50000;
      o += "๐Ÿ’—".repeat(Math.floor(r / 10000));
      r %= 10000;
      o += "๐Ÿ’“".repeat(Math.floor(r / 5000));
      r %= 5000;
      o += "โฃ".repeat(Math.floor(r / 2000));
      r %= 2000;
      o += "๐Ÿ’•".repeat(Math.floor(r / 1000));
      r %= 1000;
      o += "๐Ÿ’".repeat(Math.floor(r / 500));
      r %= 500;
      o += "๐Ÿ’š".repeat(Math.floor(r / 200));
      r %= 200;
      o += "๐Ÿงก".repeat(Math.floor(r / 100));
      r %= 100;
      o += "๐Ÿ’œ".repeat(Math.floor(r / 50));
      r %= 50;
      o += "๐Ÿ’™".repeat(Math.floor(r / 10));
      r %= 10;
      o += "๐Ÿ’›".repeat(Math.floor(r / 5));
      r %= 5;
      o += "โค".repeat(r);
      return o;
    }
    
    console.log(numbertoheart(500));  // Prints: ๐Ÿ’
    console.log(numbertoheart(600));  // Prints: ๐Ÿ’๐Ÿงก
    console.log(numbertoheart(5000)); // Prints: ๐Ÿ’“
    console.log(numbertoheart(6000)); // Prints: ๐Ÿ’“๐Ÿ’š
    Login or Signup to reply.
  2. The problem is that after you add some hearts to the result, you don’t subtract their values from r.

    For instance, r % 1000 is 500, so you display one ๐Ÿ’. But r % 200 is 100, so you also display ๐Ÿงก.

    You should reduce r to the remainder of the value of that emoji after that loop. You can achieve that in your code be reusing r instead of using $ in each loop.

    function numbertoheart(r) {
      if (r <= 0) return "";
      for (var o = "", f = 0; f < Math.floor(r / 100000); f++) o += "๐Ÿ’Ÿ";
      for (r = r % 100000, f = 0; f < Math.floor(r / 50000); f++) o += "๐Ÿ’–";
      for (r = r % 50000, f = 0; f < Math.floor(r / 10000); f++) o += "๐Ÿ’—";
      for (r = r % 10000, f = 0; f < Math.floor(r / 5000); f++) o += "๐Ÿ’“";
      for (r = r % 5000, f = 0; f < Math.floor(r / 2000); f++) o += "โฃ";
      for (r = r % 2000, f = 0; f < Math.floor(r / 1000); f++) o += "๐Ÿ’•";
      for (r = r % 1000, f = 0; f < Math.floor(r / 500); f++) o += "๐Ÿ’";
      for (r = r % 500, f = 0; f < Math.floor(r / 200); f++) o += "๐Ÿ’š";
      for (r = r % 200, f = 0; f < Math.floor(r / 100); f++) o += "๐Ÿงก";
      for (r = r % 100, f = 0; f < Math.floor(r / 50); f++) o += "๐Ÿ’œ";
      for (r = r % 50, f = 0; f < Math.floor(r / 10); f++) o += "๐Ÿ’™";
      for (r = r % 10, f = 0; f < Math.floor(r / 5); f++) o += "๐Ÿ’›";
      for (r = r % 5, f = 0; f < r; f++) o += "โค";
      return o
    }
    
    console.log(numbertoheart(500));
    console.log(numbertoheart(600));
    console.log(numbertoheart(5000));
    console.log(numbertoheart(6000));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search