銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟

Posted 鍓嶇鏃╄璇?/a> 銆愮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟相关的知识,希望对你有一定的参考价值。

鍓嶈█

浣犺緭鍏ョ殑瓒婂锛屼綘鐨勮В鍐抽棶棰樼殑宸ュ叿绠卞氨瓒婁赴瀵屻€備粖鏃ユ棭璇绘枃绔犵敱@鎺掗缈昏瘧鎶曠锛孏itlab@samanthaming鎺堟潈鍒嗕韩銆?/p>

姝f枃浠庤繖寮€濮嬶綖锝?/p>

鐜板湪鏈変簡ES6鐨凪ath.sign 馃憦锛屽垽鏂竴涓暟瀛楃殑灏卞彉寰楄秴绾х畝鍗曚簡锛屽畠鍙互鏄剧ず鍑烘暟瀛楁槸姝f暟銆佽礋鏁拌繕鏄浂銆?/p>

 
   
   
 
  1. const positive = 5;

  2. const negative = -5;

  3. const zero = 0;


  4. Math.sign(positive); // 1

  5. Math.sign(negative); // -1

  6. Math.sign(zero); // 0

Math.sign 杩斿洖鍊?/h4>

Math.sign()鏈?涓彲鑳界殑杩斿洖鍊笺€?/p>

 
   
   
 
  1. 1 // positive number

  2. -1 // negative number

  3. 0 // positive zero

  4. -0 // negative zero

  5. NaN // not a number

 
   
   
 
  1. Math.sign(8); // 1

  2. Math.sign(-8); // -1


  3. Math.sign(0); // 0

  4. Math.sign(-0); // -0


  5. Math.sign(NaN); // NaN

  6. Math.sign('hello'); // NaN

  7. Math.sign(); //NaN

娉ㄦ剰锛屼紶閫掔粰杩欎釜鍑芥暟鐨勫弬鏁板皢琚殣寮忚浆鎹负鏁板瓧绫诲瀷銆?/p>

Math.sign Gotcha

涓€涓父瑙佺殑璇尯鏄涓篗ath.sign杩斿洖杞崲鍚庣殑鍙傛暟鍊笺€侻ath.sign鍙繑鍥炰竴涓暟瀛楃殑绗﹀彿銆傚畠骞朵笉杩斿洖鍊笺€?/p>

 
   
   
 
  1. Math.sign(-8);


  2. // 鉁?return -1

  3. // 鉂?It doesn't return -8

Math.sign vs 姣旇緝

绀惧尯閲屾彁鍑轰簡涓€涓緢濂界殑闂锛?/p>

鏃㈢劧鍙互鐢ㄦ瘮杈冭繍绠楃锛屼负浠€涔堣繕瑕佺敤Math.sign锛?/p>

 
   
   
 
  1. if (number > 0) {

  2. // Positive

  3. } else {

  4. // Negative

  5. }

versus
 
   
   
 
  1. if (Math.sign(number) > 0) {

  2. // Positive

  3. } else {

  4. // Negative

  5. }

瀹為檯涓婏紝濡傛灉浣犲彧鏄鏌ュ竷灏旂姸鎬侊紝閭d箞鎴戝彧闇€瑕佷娇鐢ㄦ瘮杈冭繍绠楃锛岃€屼笉鏄娇鐢∕ath.sign銆備絾Math.sign鐨勯棯鍏夌偣鍦ㄤ簬瀹冭繑鍥炰竴涓暟瀛楀€笺€傝繖鎰忓懗鐫€浣犲彲浠ヨ繘琛岃绠椼€?/p>

 
   
   
 
  1. const number = 5;


  2. number > 0; // true

  3. Math.sign(number); // 1

鐢∕ath.sign瑙e喅涓€涓畻娉曟寫鎴?/h4>

鎵€浠ワ紝瀹冭鎴戣В鍐充簡杩欎釜绠楁硶鎸戞垬锛?鍙嶈浆涓€涓暣鏁?

 
   
   
 
  1. Input: 123;

  2. Output: 321;


  3. Input: -123;

  4. Output: -321;

 
   
   
 
  1. function reverseInteger(num) {

  2. const numArray = Math.abs(num) // Get the absolute value of our number > 321

  3. .toString() // Convert our number to a string > '321'

  4. .split('') // Convert our string of numbers to an array > [3, 2, 1]

  5. .reverse() // Reverse our array of numbers > [1, 2, 3]

  6. .join(''); // Convert our array back to a string > 123


  7. const sign = Math.sign(num); // -1

  8. return numArray * sign;

  9. // Multiply our reverse string with the sign will produce the correct reverse number

  10. }


  11. reverseInteger(-321); // -123

杩欓亾绠楁硶棰樻潵鑷簬Leetcode鐨勩€婂弽杞暣鏁般€嬨€備负浜嗙畝鍖栨垜浠殑婕旂ず锛屾垜鎶婅繖閬撻鐨勮姹傜紪杈戜簡涓€涓嬨€傝鐪嬪疄闄呯殑瑙f硶锛岃繖閲屾湁涓€閬撴潵鑷狜loia5tqd001銆?/p>

杩欏叾瀹炴槸鎴戠涓€娆″彂鐜拌繖涓嚱鏁般€傝繖涔熸槸鎴戝枩娆㈢湅鍒汉鐨勮В鍐虫柟妗堢殑鍘熷洜銆傜湅鍒汉鏄€庝箞瑙e喅鐨勶紝鎬绘槸寰堟湁鎰忔€濄€傚嵆浣胯В鍐虫柟娉曚笉濂斤紝鎴戜篃浼氱湅閭d簺锛屽洜涓哄畠鏁欐垜濡備綍閬垮厤馃槈銆傛病鏈夌煡璇嗘槸娴垂鐨勷煉€傛墍鏈夎繖浜涢兘鏄墿灞曟垜鐨勫伐鍏风銆傚氨鍍忛偅浜涙満鍣ㄥ涔犱竴鏍凤紝浣犺緭鍏ョ殑鏁版嵁瓒婂锛屽畠灏辫秺濂姐€傛垜鎯虫垜鐨勫ぇ鑴戜篃鏄繖鏍风殑銆傛垜闇€瑕佺湅鍒板緢澶氳В鍐虫柟妗堬紝鎵嶈兘璁╂垜鍙樺緱鏇村ソ馃槃馃槃

杩欏氨鏄负浠€涔堝湪鎴戠殑寰堝鑺辩诞涓紝鎴戦兘浼氭秹鍙婂埌涓嶅悓鐨勮В棰樻柟娉曘€傚洜涓烘案杩滄病鏈変竴涓渶濂界殑鏂规硶銆傛渶濂界殑鏂规硶鎬绘槸瑕佺湅鎯呭喌鑰屽畾銆備綘鐨勫伐鍏风瓒婂ぇ锛屼綘灏辫秺鏈夊彲鑳芥壘鍒版渶濂界殑鏂规硶馃憤銆?/p>

璐熼浂

鎵€浠ワ紝浣犲彲鑳戒細娉ㄦ剰鍒癕ath.sign杩斿洖鐨勬槸璐熸暟0銆?/p>

 
   
   
 
  1. Math.sign(0); // 0

  2. Math.sign(-0); // -0

鑰屼綘鐨勪笅涓€涓棶棰樻槸锛岃繖涓礋闆跺埌搴曟槸浠€涔堬紵鍦ㄤ綘涓嶆噦JS鐨凨yle Simpson瑙i噴寰楁渶娓呮銆?/p>

鐜板湪锛岄櫎浜嗗鏈笂鐨勭悙浜嬶紝鎴戜滑涓轰粈涔堣鐢ㄨ礋闆跺憿锛?/p>

鍦ㄦ煇浜涘簲鐢ㄤ腑锛屽紑鍙戜汉鍛樼敤涓€涓€肩殑澶у皬鏉ヨ〃绀轰竴涓俊鎭紙姣斿姣忓抚鍔ㄧ敾鐨勮繍鍔ㄩ€熷害锛夛紝骞朵娇鐢ㄨ繖涓暟瀛楃殑绗﹀彿鏉ヨ〃绀哄彟涓€涓俊鎭紙姣斿杩愬姩鐨勬柟鍚戯級銆?/p>

鍦ㄨ繖浜涘簲鐢ㄤ腑锛屼妇涓€涓緥瀛愶紝濡傛灉涓€涓彉閲忓埌杈鹃浂鐐规椂灏卞け鍘讳簡绗﹀彿锛岄偅涔堝湪瀹冨埌杈鹃浂鐐逛箣鍓嶏紝浣犲氨浼氬け鍘诲畠绉诲姩鏂瑰悜鐨勪俊鎭€備繚鐣欓浂鐨勭鍙峰彲浠ラ槻姝㈡綔鍦ㄧ殑涓嶉渶瑕佺殑淇℃伅涓㈠け銆?/p>

Math.sign娴忚鍣ㄦ敮鎸?/h4>

瀵规墍鏈夌幇浠g殑鏀寔閮藉緢涓嶉敊銆傚彲鎯滅殑鏄紝Internet Explorers澶椂楂︿簡锛屼笉鑳借窡鍏朵粬浜轰竴璧风帺鑰嶏紝鎵€浠ユ病鏈夋敮鎸侀偅閲屻€?/p>

IE鐨勪唬鐮佽姳绲?/h4>

浣嗕笉鐢ㄦ媴蹇冿紝杩欓噷鏈変竴涓唬鐮佺墖娈靛彲渚涢€夋嫨銆傝繖娈典唬鐮佸彲浠ュ湪IE娴忚鍣ㄥ拰鑰佺増鏈殑娴忚鍣ㄤ笂浣跨敤馃憤銆?/p>

 
   
   
 
  1. const positive = 5;

  2. const negative = -5;

  3. const zero = 0;


  4. positive === 0 ? positive : positive > 0 ? 1 : -1; // 1

  5. negative === 0 ? negative : negative > 0 ? 1 : -1; // -1

  6. zero === 0 ? zero : zero > 0 ? 1 : -1; // 0

Math.sign Polyfill

鎴栬€呯户缁娇鐢∕ath.sign锛屽彧闇€浠嶮DN涓坊鍔犺繖涓狿olyfill灏卞彲浠ヤ簡

 
   
   
 
  1. f (!Math.sign) {

  2. Math.sign = function(x) {

  3. return (x > 0) - (x < 0) || +x;

  4. };

  5. }

璧勬簮

  • MDN Web Docs - Math.sign()

  • Stack Overflow: Number sign in javascript

  • Leetcode Solution: Reverse an integer

  • Math.sign MDN Polyfill

  • Stack Overflow: How to check the value given is a positive or negative integer?

  • Stack Overflow: JS Negative Number

涓轰綘鎺ㄨ崘







以上是关于銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟的主要内容,如果未能解决你的问题,请参考以下文章

鏂囨。澶у笀 2016 锛氬湪鐢佃剳鍜屽ご鑴戜腑蹇€熸壘鍒版枃妗g殑鏂囦欢绠$悊杞欢

251.Spring Boot+Spring Security锛氬熀浜嶶RL鍔ㄦ€佹潈闄愶細鑷畾涔塅ilter

銆岀71鏈熴€? 鐖櫕鎶€鏈?鎶撳寘

銆岀81鏈熴€? 鏋舵瀯鍒嗘瀽 -Tomcat 璇锋眰娴佺▼

6銆?0閬揓AVA鍩虹缂栫▼缁冧範棰樿窡绛旀