-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode 1-13.js
67 lines (63 loc) · 1.54 KB
/
Code 1-13.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
};
let initialize=()=>{
navigation.x=0;
navigation.y=0;
navigation.z=0;
};
let calibrateX=()=>{
for(let i=1; i<=12;i++){
let signal=checkSignal();
if(signal!==undefined){
navigation.x=signal;
break;
}
}
};
loadModuleByName("life-support");
loadModuleByName("propulsion",true);
loadModuleByName("navigation",true);
loadModuleByName("communication",true);
resetLARRY();
setMessage(JSON.stringify(navigation));
activateBeacon();
calibrateX();