-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode 1-11.js
52 lines (48 loc) · 1.27 KB
/
Code 1-11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let powerOn=() => {
if(!ship.powerOn)ship.powerOn=true;
}
let countModules=() =>{
return availableModules.length;
}
let countEssential=() =>{
return availableModules.filter(function(module){
return module.essential;
} ).length;
}
let loadModule=(index,essential)=>{
if(index<0)throw new Error("Index must be positive");
let modCount=countModules();
if(index>modCount)throw new Error("Index must wthin 0 and " +modCount);
let module=availableModules[index];
module.enabled=true;
if(essential!==undefined)module.essential=essential;
ship.modules.push(module);
}
let findModuleIndex=(moduleName) =>{
return availableModules.findIndex(function(module){
return module.name==moduleName;
});
}
let loadModuleByName=(name,essential)=>{
loadModule(findModuleIndex(name,essential));
}
let resetLARRY=()=>{
for(let i=0; i<10;i++)LARRY.quack();
}
let setMessage=(message)=>{
radio.message=message;
}
let activateBeacon=()=>{
radio.beacon=true;
}
let setFrequency=()=>{
let range=radio.range;
radio.frequency=(range.low+range.high)/2;
}
loadModuleByName("life-support");
loadModuleByName("propulsion",true);
loadModuleByName("navigation",true);
loadModuleByName("communication",true);
resetLARRY();
setMessage(JSON.stringify(navigation));
activateBeacon();