Spocoman

01. Old Books

Dec 28th, 2021 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function oldBooks(input) {
  2.     let book = input[0];
  3.     let index = 0;
  4.     let command;
  5.  
  6.      while(true) {
  7.         command = input[++index];
  8.         if (command === "No More Books" || command === book) {
  9.             if (command === book) {
  10.                 console.log(`You checked ${--index} books and found it.`);
  11.             } else {
  12.                 console.log(`The book you search is not here!\nYou checked ${--index} books.`);
  13.             }
  14.             return;
  15.         }
  16.     }
  17. }
  18.  
  19. Решение с for:
  20.  
  21. function oldBooks(input) {
  22.     let book = input[0];
  23.  
  24.     for (let i = 1; i < Number.MAX_SAFE_INTEGER; i++) {
  25.         let command = input[i];
  26.         if (command === "No More Books" || command === book) {
  27.             if (command === book) {
  28.                 console.log(`You checked ${--i} books and found it.`);
  29.             } else {
  30.                 console.log(`The book you search is not here!\nYou checked ${--i} books.`);
  31.             }
  32.             return;
  33.         }
  34.     }
  35. }
Add Comment
Please, Sign In to add comment