Code Monkey home page Code Monkey logo

Comments (4)

miniflycn avatar miniflycn commented on June 19, 2024

这个是最简洁的

from exercise2.

chencl1986 avatar chencl1986 commented on June 19, 2024

添加注释,方便理解:

function sumStrings(a,b){
    var res = '', // 存储最终结果
    c = 0; // 假设想加结果>=10,那么把10位缓存起来
  // 将字符串切割成数组,每一位单独相加
  a = a.split('')
  b = b.split('')

  // 不断循环相加,直到所有数字都加完为止
  while (a.length || b.length || c) {
    /* 
      一、c为Boolean:
        1. true与数字n相加,相当于1+n
        2. false与数字n相加,相当于0+n
      二、[Bitwise NOT (~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
        1. 数字进行~~运算,例如~~3的结果为3
        2. a和b两个数组长度可能不一致,其中一个会先清空
        3. 假设a为[],a.pop()为undefined
        4. ~~undefined的结果为0
      三、c += ~~a.pop() + ~~b.pop() 可以计算出每一位相加的结果
    */
    c += ~~a.pop() + ~~b.pop()
    /* 
      1. c的结果可能>=10,但每一位只能存储0-9的数字,因此要用c % 10取个位数
      2. 每位数要存储在结果的首尾
    */
    res = (c % 10) + res
    /* 
      判断c是否>=10,并进行下一次相加
    */
    c = c > 9
  }

  return res.replace(/^0+/, '')
}

from exercise2.

miniflycn avatar miniflycn commented on June 19, 2024

添加注释,方便理解:

function sumStrings(a,b){
    var res = '', // 存储最终结果
    c = 0; // 假设想加结果>=10,那么把10位缓存起来
  // 将字符串切割成数组,每一位单独相加
  a = a.split('')
  b = b.split('')

  // 不断循环相加,直到所有数字都加完为止
  while (a.length || b.length || c) {
    /* 
      一、c为Boolean:
        1. true与数字n相加,相当于1+n
        2. false与数字n相加,相当于0+n
      二、[Bitwise NOT (~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
        1. 数字进行~~运算,例如~~3的结果为3
        2. a和b两个数组长度可能不一致,其中一个会先清空
        3. 假设a为[],a.pop()为undefined
        4. ~~undefined的结果为0
      三、c += ~~a.pop() + ~~b.pop() 可以计算出每一位相加的结果
    */
    c += ~~a.pop() + ~~b.pop()
    /* 
      1. c的结果可能>=10,但每一位只能存储0-9的数字,因此要用c % 10取个位数
      2. 每位数要存储在结果的首尾
    */
    res = (c % 10) + res
    /* 
      判断c是否>=10,并进行下一次相加
    */
    c = c > 9
  }

  return res.replace(/^0+/, '')
}

点赞👍

from exercise2.

y-x-n avatar y-x-n commented on June 19, 2024

膜拜

from exercise2.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.