在Javascript中获取没有modulo(%)运算符的余数,占用 - / +符号 [英] Getting a remainder without the modulo (%) operator in Javascript, accounting for -/+ sign

查看:56
本文介绍了在Javascript中获取没有modulo(%)运算符的余数,占用 - / +符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于家庭作业,我需要在使用内置模数(%)运算符将num1除以num2之后返回余数。我能够通过以下代码获得大多数测试,但我仍然坚持如何考虑给定数字的 - / +符号。我需要随身携带num1上的任何一个标志,如果num2是负数,也会返回一个正数 - 这让我想起如何做到这一点...... :)任何清晰度都会非常感激!我不是在寻找这里的直接答案,更多的是我似乎错过了一些明显的东西......也许我需要一种新方法?

For a homework assignment, I need to return the remainder after dividing num1 by num2 WITHOUT using the built-in modulo (%) operator. I'm able to get most tests to pass with the following code, but I'm stuck on how to account for -/+ signs of the given numbers. I need to carry over whichever sign is on num1, and also return a positive number if the num2 is negative - it's blowing my mind how to do this... :) Any clarity would be greatly appreciated! I'm not exactly looking for the straight up answer here, more that I seem to be missing something obvious... Maybe I need a new approach?

    function modulo(num1, num2) {
      if (num1 === 0) {
        return 0;
      }
      if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
        return NaN;
      }
      if (num1 < num2) {
        return num1;
      }
      if (num1 > 0 && num2 > 0) {
        var counter = num1;
      while (counter >= Math.abs(num2)) {
        counter = counter - num2;
      }
      return counter;
      }
    }
    var output = modulo(25, 4);
    console.log(output); // 1


推荐答案

你可能会过度思考这个问题。你基本上在你的问题中陈述了解决方案:

You might be overthinking this. You basically stated the solution in your question:


我需要随身携带num1上的任何一个符号,如果是num2是负数

I need to carry over whichever sign is on num1, and also return a positive number if the num2 is negative

第二部分不准确,但我怀疑你只是错过了。当 num2 为负时,应返回正数,除非 num1 为负数。

The second part isn't accurate, but I suspect you just misspoke. A positive number should be returned when num2 is negative unless num1 is negative.

无论如何,重要的一点是,如果 num1 为负数,结果将为负数,否则结果将为要乐观。 num2 的符号将被丢弃。

At any rate, the important takeaway is that if num1 is negative the result will be negative, and otherwise the result will be positive. The sign of num2 is discarded.

启动您编写的代码(其他代码将快速指向out不是最简单的解决方案),修复方法是使用两个数字的绝对值计算余数,然后将 num1 的原始符号应用于结果。

Starting the code you've written (which others will be quick to point out isn't the simplest solution), the fix is to compute the remainder using both numbers' absolute values, and then apply num1's original sign to the result.

function modulo(num1, num2) {
  var sign = num1 < 0 ? -1 : 1;
  var dividend = Math.abs(num1);
  var divisor = Math.abs(num2);

  if (dividend === 0) {
    return 0;
  }
  if (dividend === 0 || isNaN(dividend) || isNaN(divisor)) {
    return NaN;
  }
  if (dividend < divisor) {
    return sign * dividend;
  }
  
  var counter = dividend;
  while (counter >= divisor) {
    counter = counter - divisor;
  }
  return sign * counter;
}

console.log( 25 %  4, modulo( 25,  4));
console.log(-25 %  4, modulo(-25,  4));
console.log( 25 % -4, modulo( 25, -4));
console.log(-25 % -4, modulo(-25, -4));

.as-console-wrapper{min-height:100%;}

这篇关于在Javascript中获取没有modulo(%)运算符的余数,占用 - / +符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆