JavaScript  . 12   
 


    ,   ,     catch?   ,     , , ,    JavaScript.    12    ,    ,  ,       .  48  :  ,   .    Node.js 24.19.0   ,     .  ,   , , , , this, JSON  Promise.             .





 

JavaScript  . 12   





   





   


   ,  ,      .        .      :    ,    ,        .       ,     .

,      ,  if  ,       .        .     ,     ,     .        : Node.js       JavaScript.

     .        .     ,  ,    .      , JSON   .    ,       ; ,       .




   


    Node.js v24.19.0.      LTS- Node.js   ;      ,      .      .    npm-,       .

         ,   ,  c05-fixed.mjs.  .mjs   ES-.    ,     .        node --unhandled-rejections=strict c05-fixed.mjs.       .   :        .

    node:assert/strict. assert.equal   , assert.deepEqual  , assert.throws   ,  assert.rejects   Promise,   .     .    ,    AssertionError;      TypeError,      .

    ,   .           Word.         ;  JavaScript      ,       .          .        :       ,   .

   ,    .     ,   ,       .    : Promise       .            .




 ,  


        .        .      ,   ,  ,      .     :    ,   .

    .        ,     .         ,            .  :      id,      .

    ,    .    ,        .       .    ,  :   ,  ,        .




  


    :       ;     ;    ,      ;   undefined    ?    ,         .

  :     ;       ;      ;  undefined       .    .        ,      .




   


    , ,       5.1.      ,     .     ,      :     .         .

         .       .      ;             .     .




 


1.     (#start-1)

2.    (#start-2)

3.      (#start-3)

4.      (#start-4)

5.    (#litres_trial_promo)

6.      (#litres_trial_promo)

7.    (#litres_trial_promo)

8.      (#litres_trial_promo)

9.     (#litres_trial_promo)

10.     JSON (#litres_trial_promo)

11.      (#litres_trial_promo)

12.     catch (#litres_trial_promo)




 





 1.    





1.   


        ,    .




   ,  ,   return;   for   .

      .     '27',    '5'.    :     .    ,      .     32,    ,    '275'.

   :      09;       .       0  10000 . , , , ,        .    null,      .   .

  +    .     .         ,   .     :    .  '27' + 5   .      ;     .

Number(text),   new,      .      :     ,         .    ,     .   ,  ,      .

 trim()    .  /^[0-9]+$/       : [0-9]  , +     , ^  $  .  test  true  false. Number.isSafeInteger ,     ;       . null    ,     .




1.    c01-bug.mjs


const morning = '27';

const extra = '5';

const total = morning + extra;



console.log(total);

console.log(typeof total);




1.  



 

275

string

    '27'  '5'.   +      :      .       ,      string.

  :   .   ,   .  typeof   ,           .




1.    c01-fixed.mjs


import assert from 'node:assert/strict';



function readCount(raw) {

if (typeof raw !== 'string') return null;

const text = raw.trim();

if (!/^[0-9]+$/.test(text)) return null;

const value = Number(text);

if (!Number.isSafeInteger(value)) return null;

if (value > 10000) return null;

return value;

}



function sumCounts(left, right) {

const a = readCount(left);

const b = readCount(right);

if (a === null || b === null) return null;

return a + b;

}



const total = sumCounts('27', '5');

console.log(total);

console.log(typeof total);



assert.equal(total, 32);

assert.equal(sumCounts(' 004 ', '6'), 10);

assert.equal(sumCounts('0', '0'), 0);

assert.equal(sumCounts('10000', '10000'), 20000);

for (const bad of ['', ' ', '-1', '2.5', '1e2']) {

assert.equal(sumCounts(bad, '1'), null);

}

for (const bad of ['0x10', '10001', '2x', 7]) {

assert.equal(sumCounts('1', bad), null);

}




1.   



 

32

number

     ;      .  === null   .     20000,          .         : '004'  4.

     :    ,  ,      .         .      :   '2x'      .

       ;      .




 1.1


     : ['18', '0', '7'].   25.              100 .              09     010000.   ;  , , ,      .    0;          :  null.    .

 1.1 (#litres_trial_promo)

 1.1 (#litres_trial_promo)




 1.2


       '008'   '3': Q-008, Q-009, Q-010,    .         .    0  999,    0  20.     .        999  null.

 1.2 (#litres_trial_promo)

 1.2 (#litres_trial_promo)

   (#navigation)




 2.   





2.   


  ,        .




,  ,  ,     .

      .  blankLines      :  0  6,   .      .       null,   .    2    0;    .

   ,         .         null   .  '0', false, , NaN       .      .

 ||       ,     .    ,   truthy,  .  falsy  0, false,  , NaN, null  undefined.         .       :    .

 ??    :       null  undefined .     .  ,   .     ,     .     ,  NaN;        .

   Number.isInteger    ,   .     .     :   ,   ,   .  ??  ||        ;        .




2.    c02-bug.mjs


function blankLines(value) {

return value || 2;

}



console.log(blankLines(0));

console.log(blankLines(undefined));




2.  



 

2

2

    0  ,   ||  falsy.     2.    undefined  falsy,     .       .

         :     .  ,   ,      .        .




2.    c02-fixed.mjs


import assert from 'node:assert/strict';



function blankLines(value) {

const chosen = value ?? 2;

if (!Number.isInteger(chosen)) return null;

if (chosen < 0 || chosen > 6) return null;

return chosen;

}



console.log(blankLines(0));

console.log(blankLines(undefined));



assert.equal(blankLines(0), 0);

assert.equal(blankLines(5), 5);

assert.equal(blankLines(6), 6);

assert.equal(blankLines(null), 2);

assert.equal(blankLines(), 2);

for (const bad of [-1, 7, 0.5, '0', false, NaN]) {

assert.equal(blankLines(bad), null);

}

assert.equal(blankLines(Infinity), null);




2.   



 

0

2

  ,   ,       .   .         .  assert   ,         .

    ,  null  undefined  .  null    ,    ,      .      ||  ??:      .

      falsy-     .




 2.1


  : {captions: false, title: ''}.    ;   .  captions   boolean;  true.  title   ,  ;  'Preview'.     null  undefined.        ,   null.        .  false       .

 2.1 (#litres_trial_promo)

 2.1 (#litres_trial_promo)




 2.2


      :   ,  ,  4.   null  undefined.         0  8;   ,  null     .    .   undefined   0  0.

 2.2 (#litres_trial_promo)

 2.2 (#litres_trial_promo)

   (#navigation)




 3.     





3.   


    ,      .




        ;  ,    .

      : [8, 35, 120, 4] .    ;        .    4, 8, 35, 120.     ,      .

      0  600 . ,     . , NaN, Infinity,        .      ,       .

 sort()         ,    UTF-16.          :  '120'   '1'    '35', '4'  '8'.         :  ,    .

   , sort   ,  .   (a, b) => a - b    a  b,      .     a  b,   ,      .         .

   sort    ,    .     .       .  [...values]       values;   ,    .       ;       .




3.    c03-bug.mjs


const minutes = [8, 35, 120, 4];

const report = minutes.sort();



console.log(report.join(', '));

console.log(minutes.join(', '));

console.log(report === minutes);




3.  



 

120, 35, 4, 8

120, 35, 4, 8

true

 join(', ')     ;     .     .  ,     .  : report  minutes   ,      .

   ,   ,       .    ,  ,     .     ,     .




3.    c03-fixed.mjs


import assert from 'node:assert/strict';



function orderedMinutes(values) {

const copy = [...values];

copy.sort((a, b) => a - b);

return copy;

}



const minutes = [8, 35, 120, 4];

const report = orderedMinutes(minutes);

console.log(report.join(', '));

console.log(minutes.join(', '));

console.log(report === minutes);



assert.deepEqual(report, [4, 8, 35, 120]);

assert.deepEqual(minutes, [8, 35, 120, 4]);

assert.notEqual(report, minutes);

assert.deepEqual(orderedMinutes([]), []);

assert.deepEqual(orderedMinutes([7]), [7]);

const bounds = [600, 0, 600, 0];

assert.deepEqual(orderedMinutes(bounds), [0, 0, 600, 600]);

assert.deepEqual(bounds, [600, 0, 600, 0]);

const empty = [];

assert.notEqual(orderedMinutes(empty), empty);




3.   



 

4, 8, 35, 120

8, 35, 120, 4

false

      ,        .     .        ,        .

      :         .         :    ,      .

    :    ,   .




 3.1


  [28, 6, 103, 6]     .  [6, 6]:        .       0600.     ,      .   .          .

 3.1 (#litres_trial_promo)

 3.1 (#litres_trial_promo)




 3.2


   [9, 130, 7, 16],   .         ,      .     12.5.      0600;    null.          .

 3.2 (#litres_trial_promo)

 3.2 (#litres_trial_promo)

   (#navigation)




 4.     





4.   


     callback       .




,   return;        .

        .   [' drill ', 'spare cord']    ['DRILL', 'SPARE CORD']:   ,    .     .          .

       ,   .  ,       ;       ,     .       Unicode,            .

 map                  .    callback:   ,      .     name,     .    trim  toUpperCase    map,     .

         .      :    .    ,   .         return.      return    undefined. JavaScript       .




  .


   .

   ,     (https://www.litres.ru/book/raznoe/javascript-bez-ugadyvaniia-12-razborov-neozhidannykh-rezul-tatov-74418097/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


