write a javascript multiplication function that will return two separate results

前端 未结 4 442
长情又很酷
长情又很酷 2021-01-27 01:11

As you can see from the embed below... my script is only returning one result(500). How can I rewrite my code so that I get both results? Thank you in advance for your advice.

4条回答
  •  被撕碎了的回忆
    2021-01-27 01:50

    A function can only return one thing. A generator function (function*) however can yield as many numbers as you want:

     function* multiplier() {
       yield 25 * 20;
       yield 25 * 1;
     }
    
     console.log(...multiplier())
    

    You could also turn the results into an array easily:

    const array = [...multiplier()];
    

    (No this is not the easiest way, but im trying to propagate some cool js :))

提交回复
热议问题