Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Hai lần giảm giá liên tiếp trong JavaScript: kiểm thử bằng số nguyên

Khi viết bài tập JavaScript về phần trăm, một lỗi thường gặp là cộng hai mức giảm trước khi tính. Ví dụ, giảm 25% rồi giảm thêm 20% khiến giá từ 100 xuống 75, rồi từ 75 xuống 60. Mức giảm thứ hai tính trên 75, vì vậy tổng mức giảm là 40%. Viết hàm xử lý một bước Hàm nhận số tiền đã quy đổi sang đơn vị nhỏ nhất, chẳng hạn cent, và phần trăm nguyên từ 0 đến 100. Ví dụ dùng các số nguyên nhỏ, đủ để…

When practicing JavaScript percentage calculations, a common mistake is to add two discounts before computing the final value. For example, applying a 25% reduction followed by an additional 20% discount reduces a price of 100 to 75, then to 60. The second discount is calculated on the reduced 75, making the total discount 40%. Write a one-step function To test, the function accepts an amount expressed in the smallest unit (such as cents), and a whole number percentage ranging from 0 to 100.

Using small integers keeps the focus on order of operations and rounding rules. Import the assert module for testing. Define the afterDiscount function to calculate the discounted amount. Set a starting amount of 10,000 cents. Call the function twice, first with a 25% discount, then a 20% discount on the result. Assert that the intermediate and final amounts match the expected values of 7,500 and 6,000 cents, respectively.

Verify that applying no discount leaves the amount unchanged. Assert that a 100% discount reduces any amount to zero. Check that applying a discount twice does not change the amount if the second discount is 10% of the already discounted value. To validate rounding, test the scenario where a price of 5 cents is discounted twice by 10%.

The rule requires the final result to remain 5 cents. If an odd number is retained, multiplying 5 by 0.9 twice results in 4.05 cents, which should round to 4 cents. Different rounding rules apply in real-world applications. Determine which rule to follow and test the input range. This article was written with AI assistance.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Predicting Injury Counts in Motor Vehicle Collisions

What is Playwright? Playwright is an end to end testing tool. End to end refers to the practice of testing an application frontend and backend. End to end mimics user behavior in an automated fashion.

  • Playwright is used for end-to-end testing of applications.
  • Flaky tests occur due to unpredictable computers and multiple tests on a single server.
  • Resource Intensive Queue (RIQ) prevents deadlocks and out-of-memory errors in tests.

MCP Server Backup and Disaster Recovery: A Practical Plan

Quick answer: Backups and high availability solve different problems, and an MCP deployment needs both. Failover moves traffic to a healthy node when one crashes, but it does not protect against a…

  • Failover moves traffic to healthy node, not logical failure protection
  • Backup stateful parts like registry, routing config, and metadata
  • 3-2-1 backup strategy with off-site storage in different region

DNS Tunneling, Explained: How Data Hides Inside Ordinary DNS Lookups

Most company firewalls say no to almost everything going out. But they nearly always say yes to one thing: DNS, the lookup that turns github.com into an IP address.

  • Attackers hide data in DNS lookups to bypass firewalls
  • Malware encodes stolen data in DNS queries using Base32 or hex
  • Detect DNS tunneling by spotting long labels, high entropy, unique domain names, and TXT records

Tencent Hy4's Weights Are Finally Downloadable: 770B Parameters, 1.55TB, and a License Trap

Tencent Hy4's Weights Are Finally Downloadable: 770B Parameters, 1.55TB, and a License Trap On November 4, Tencent announced Hy4: 770B parameters, 49B active, a million-token context.

  • Tencent unveiled Hy4 language model with 770 billion parameters on November 4
  • Hy4's weights became downloadable on November 20, totaling 1.55TB
  • Licensing terms under Tencent Hy License restrict model usage

Gravity Bridge lost $5.4M because one key could say withdraw(all)

On May 30, 2026, Gravity Bridge lost about $5.4M. Not to a reentrancy bug. Not to an oracle manipulation, or a flash loan, or a clever math error.

  • Gravity Bridge lost $5.4M in May 2026 due to key compromise.
  • Attacker converted stolen assets into ETH, some to Binance.
  • Incident highlights need for better operational security and key management.

More from Monday 23 November →