Advertisement
Spocoman

The Pyramid Of King Djoser

Jan 11th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function thePyramidOfKingDjoser (basis, increment) {
  2.     let step = 0;
  3.     let stone = 0;
  4.     let marble = 0;
  5.     let lapis = 0;
  6.     let gold = 0;
  7.  
  8.     for (let i = basis; i > 0; i -= 2) {
  9.         let size = i * i;
  10.         step++;
  11.         if (i < 3) {
  12.             gold = size * increment;
  13.         } else {
  14.             stone += (i - 2) * (i - 2) * increment;
  15.             let decorate = (i * 4 - 4) * increment;
  16.             if (step % 5 !== 0) {
  17.                 marble += decorate;
  18.             } else {
  19.                 lapis += decorate;
  20.             }
  21.         }
  22.     }
  23.     console.log(`Stone required: ${Math.ceil(stone)}`);
  24.     console.log(`Marble required: ${Math.ceil(marble)}`);
  25.     console.log(`Lapis Lazuli required: ${Math.ceil(lapis)}`);
  26.     console.log(`Gold required: ${Math.ceil(gold)}`);
  27.     console.log(`Final pyramid height: ${Math.floor(step * increment)}`);
  28. }
  29.  
  30. РЕШЕНИЕ С КОЛЕКЦИЯ И ТЕРНАРЕН ОПЕРАТОР И FOR IN:
  31.  
  32. function thePyramidOfKingDjoser(basis, increment) {
  33.     let info = { 'stone': 0, 'marble': 0, 'lapis': 0, 'gold': 0, 'step': 0 };
  34.  
  35.     for (let i = basis; i > 0; i -= 2) {
  36.         info['step']++;
  37.         info[i < 3 ? 'gold' : 'stone'] += (i < 3 ? i : (i - 2)) ** 2 * increment;
  38.         i >= 3 ? info[info['step'] % 5 !== 0 ? 'marble' : 'lapis'] += (i * 4 - 4) * increment : info;
  39.     }
  40.  
  41.     for (i in info) {
  42.         console.log(i !== 'step' ? `${i[0].toUpperCase() + i.slice(1)}${i === 'lapis' ? ' Lazuli' : ''} required: ${Math.ceil(info[i])}` :
  43.             `Final pyramid height: ${Math.floor(info['step'] * increment)}`);
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement