Advertisement
EntropyStarRover

Technology Fundamentals Retake Mid Exam - 18 December 2018

Jun 26th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. Christmass Spirit
  2.  
  3. function xmassSpirit(input){
  4. let maxQuant=Number(input[0]);
  5. let days=Number(input[1]);
  6. let spirit=0;
  7. let cost=0;
  8.  
  9. for (let i=1; i<=days; i++){
  10.  
  11.     if (i%11===0){
  12.         maxQuant+=2;
  13.     }
  14.     if (i%2===0){
  15.         cost+=maxQuant*2;
  16.         spirit+=5;
  17.     }
  18.     if (i%3===0){
  19.         cost+=(maxQuant*5)+(maxQuant*3);
  20.         spirit+=13;
  21.     }
  22.  
  23.     if (i%5===0){
  24.         cost+=maxQuant*15;
  25.         spirit+=17;
  26.         if (i%3===0){
  27.             spirit+=30;
  28.         }
  29.     }
  30.  
  31.     if (i%10===0){
  32.         spirit-=20;
  33.         cost+=23;      
  34.     }
  35. }
  36. if (days%10==0){
  37.     spirit-=30;
  38. }
  39.  
  40. console.log(`Total cost: ${cost}`);
  41. console.log(`Total spirit: ${spirit}`)
  42. }
  43.  
  44.  
  45.  
  46. 2. Santa's List
  47.  
  48. function santasList(input){
  49.    let kidsList=input.shift()
  50.    .split("&");
  51.  
  52.    let commandsArr=input;
  53.    commandsArr.pop();
  54.  
  55.  
  56.    for (let i=0; i<commandsArr.length; i++){
  57.        let command=commandsArr[i].split(" ");
  58.        if (command.includes("Good")){
  59.            let goodKid=command[1];
  60.            if (kidsList.includes(goodKid)){
  61.                let indexOfGoodKid=kidsList.indexOf(goodKid);
  62.                kidsList.splice(indexOfGoodKid,1);
  63.            }
  64.        } else if (command.includes("Bad")){
  65.            let badKid=command[1];
  66.            if (!kidsList.includes(badKid)){
  67.                kidsList.unshift(badKid);
  68.            }
  69.        } else if(command.includes("Rename")){
  70.            let oldName=command[1];
  71.            let newName=command[2];
  72.            if (kidsList.includes(oldName)){
  73.                let indexOfOldName=kidsList.indexOf(oldName);
  74.                kidsList.splice(indexOfOldName,1,newName);
  75.            }
  76.    
  77.        } else if (command.includes("Rearrange")){
  78.            let name=command[1];
  79.            if (kidsList.includes(name)){
  80.                let indexOfName=kidsList.indexOf(name);
  81.                kidsList.splice(indexOfName,1);
  82.                kidsList.push(name);
  83.            }
  84.        }
  85. }
  86. console.log(kidsList.join(", "));
  87. }
  88.  
  89.  
  90. 3. Present Delivery
  91.  
  92. function presentDelivery(input){
  93.    let housesArr=input.shift()
  94.    .split("@");
  95.    housesArr=housesArr.map(Number);
  96.  
  97.    let jumpCommands=input;
  98.  
  99.  
  100.    let position=0;
  101.    let landedAt=0;
  102.  
  103.    for (let i=0; i<jumpCommands.length-1;i++){
  104.        let command=jumpCommands[i];
  105.        if (command.includes("Merry Xmas!")){
  106.            break;
  107.        } else {
  108.            let jumpArr=command.split(" ");
  109.            let jumpLength=Number(jumpArr[1]);
  110.            landedAt=position+jumpLength;    
  111.        
  112.            
  113.            while (landedAt>=housesArr.length){
  114.        
  115.                landedAt=landedAt-(housesArr.length);
  116.            
  117.            }
  118.  
  119.            position=landedAt;
  120.  
  121.        
  122.            if (housesArr[landedAt]>=2){
  123.            housesArr[landedAt]-=2;
  124.            } else {
  125.                console.log(`House ${landedAt} will have a Merry Christmas.`);
  126.            }
  127.        }
  128.    }
  129.    console.log(`Santa's last position was ${landedAt}.`);
  130.  
  131.     let failedHouses=0;
  132.     housesArr.forEach(house => {
  133.         if (house!==0){
  134.             failedHouses++;
  135.         }
  136.     });
  137.     if (failedHouses!==0){
  138.     console.log(`Santa has failed ${failedHouses} houses.`);
  139.     } else {
  140.         console.log(`Mission was successful.`);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement