xoxama

Page Calculator

Feb 22nd, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. >> Page Calculator
  2.  
  3. const input = ['Telerik, Java, CSS, JavaScript, PHP, HTML, CSS, Spring, Angular', '5', 'CSS'];
  4. const print = this.print || console.log;
  5. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  6.  
  7. // Write a program that accepts an array of words, a value representing words per page and a keyword and
  8. // outputs on which page the keyword can be found.
  9. // If the word is not in the list print -1.
  10.  
  11. const wordsArr = gets().split(', '); // ['Telerik', 'Java','CSS','JavaScript','PHP','HTML','CSS','Spring','Angular']
  12. const wordsPerPage = +gets(); // 2
  13. const keyWord = gets(); // CSS
  14. let whichIndex = 0;
  15. let whichPage = 0;
  16.  
  17. for (let i = 0; i < wordsArr.length; i++) {
  18. if (keyWord === wordsArr[i]) {
  19. whichIndex = i + 1;
  20. }
  21. }
  22. // print(whichIndex); // 7
  23.  
  24. whichPage = whichIndex / wordsPerPage;
  25. print(Math.ceil(whichPage));
Advertisement
Add Comment
Please, Sign In to add comment