21 Day Coding Challenge

Share Your 1 - 21 Code Here

#1

Hi guys!
This has been fun and thank you all for participating.
I’m putting up this post for anyone want to share and compare their own solutions to everyone else’s. :smile:

Remember to format it neatly use triple back-ticks ( gif for help what I mean )

2 Likes
#2
const powerOn = () => ship.powerOn = !ship.powerOn;

const countModules = () => availableModules.length;

const countEssential = () => {
  let count = 0;
  for (i of availableModules) {
    if (i.essential) {
      count++; 
    }
  }
  return count;
}

const loadModule = index => {
  const module = availableModules[index];
  if (module) {
    module.enabled = true;
    ship.modules.push(module);
  }
}

const findModuleIndex = name => {
  for (let i = 0; i < availableModules.length; i++) {
    if (availableModules[i].name === name)
      return i;
  }
  return null;
}

const resetLARRY= () => {
  for (let i = 0; i <10; i++)
    LARRY.quack();
}

const setMessage = () => radio.message = JSON.stringify(navigation);

const activateBeacon = () => radio.beacon = true;

const setFrequency = () => radio.frequency = (radio.range.low + radio.range.high) / 2;

const initialize = () => {
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
}

const calibrateX = () => {
  for (let i = 0; i < 12; i++) {
    let number = checkSignal();
    if (number) {
      navigation.x = number;
      break;
    }
  }
}

const calibrateY = () => {
  for (let i = 0; i < 60; i++) {
    let number = checkSignal();
    if (number) {
      navigation.y = number;
      break;
    }
  }
}

const calibrateZ = () => {
  for (let i = 0; i < 60; i++) {
    let number = checkSignal()
    if (number) {
      navigation.z = number;
      break;
    }
  }
}

const calibrate = () =>{
  calibrateX();
  calibrateY();
  calibrateZ();
}

const setSpeed = speed => {
  if (parseInt(speed) >= 0) {
    navigation.speed = parseInt(speed);    
  }
}

const activateAntenna = () => ship.antenna.active = true;

const sendBroadcast = () => {
  for (let i = 0; i < 100; i++) {
   broadcast();
  }
}

const configureBroadcast = () => {
  setFrequency();
  activateAntenna();
  sendBroadcast();
}

const decodeMessage = msg => {
 return msg
  .replace(/4/g, 'a')
  .replace(/3/g, 'e')
  .replace(/1/g, 'i')
  .replace(/0/g, 'o')
  .replace(/2/g, 'u')
  .replace(/5/g, 'y')
}

const returnToEarth = () => {
  let rx = broadcast("x");
  let ry = broadcast("y");
  let rz = broadcast("z");
  
  navigation.x = parseInt(decodeMessage(rx), 16);
  navigation.y = parseInt(decodeMessage(ry), 16);
  navigation.z = parseInt(decodeMessage(rz), 16);
  
}

resetLARRY();

// activateAntenna();
// sendBroadcast();


loadModule(findModuleIndex('life-support'));
loadModule(findModuleIndex('propulsion'));
loadModule(findModuleIndex('navigation'));
loadModule(findModuleIndex('communication'));
returnToEarth();

// configureBroadcast();

// setMessage();

This is what I did for the challenges. I’m excited to see how others did theirs to compare :smiley:

1 Like
#3

Hoo that’s fun :slight_smile: Thank you @cpt.waffle

Here’s what mine ended up looking like.

function resetLARRY(quackCount = 10) {
  let count = 0;
  do {
    LARRY.quack();
    count++;
  } while(count < quackCount);
}

resetLARRY();

function powerOn() {
  ship.powerOn = true;
}

const countModules = () => availableModules.length;

function countEssential() {
  return availableModules.reduce((a, i) => a + ( i.essential ? 1 : 0 ), 0);
}

function findModuleIndex(moduleName) {
  return availableModules.findIndex( x => (x.name === moduleName && "essential" in x));
}

function loadModule(index) {
  availableModules[index].enabled = true;
    availableModules[index].essential = true;
  ship.modules.push(availableModules[index]);
}

loadModule(findModuleIndex("life-support"));
loadModule(findModuleIndex("propulsion"));
loadModule(findModuleIndex("navigation"));
loadModule(findModuleIndex("communication"));

let setMessage = () => radio.message = JSON.stringify(navigation);

let activateBeacon = () => radio.beacon = true;

let setFrequency = () => radio.frequency = ((radio['range'].low + radio['range'].high) / 2);

let initialize = () => {
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
}

const calibrateX = () => calibrateNav('x', 12);
const calibrateY = () => calibrateNav('y', 60);
const calibrateZ = () => calibrateNav('z', 60);

const calibrateNav = (coordinate, checks) => {
  for(let i = 1; i <= checks; i++){
    let signal = checkSignal();
    if(signal !== undefined){
      navigation[coordinate] = signal;
      break;
    }
  }
}
function calibrate(){
  calibrateX();
  calibrateY();
  calibrateZ();
}

function setSpeed(speed) {
  if (parseInt(speed,10) >= 0) {
    navigation.speed =  parseInt(speed,10); 
  }
}

const sendBroadcast = () => {for(i = 1; i <= 100; i++) broadcast();};

const activateAntenna = () => ship.antenna.active = true;

setMessage();

function configureBroadcast() {
  setFrequency();
  activateAntenna();
  sendBroadcast();
}
configureBroadcast();

function decodeMessage(message){
  const vowelList = { 0: 'o', 1 : 'i', 2: 'u', 3: 'e', 4 : 'a', 5: 'y'};
  for ( vowel in vowelList) {
    let re = new RegExp(vowel,"g");
    message = message.replace(re, vowelList[vowel]);
  }
  return(message);
}

function returnToEarth() {
 navigation.x = parseInt(decodeMessage(broadcast('x')),16);
 navigation.y = parseInt(decodeMessage(broadcast('y')),16);
 navigation.z = parseInt(decodeMessage(broadcast('z')),16);
}
returnToEarth(); 

And as a bonus, all the ways I saw of doing Challenge #20, great work everyone!!! (They might not all be in their working state) :wink:

function decodeMessage(message){
  const vowelList = { 0: 'o', 1 : 'i', 2: 'u', 3: 'e', 4 : 'a', 5: 'y'};
  for ( vowel in vowelList) {
    let re = new RegExp(vowel,"g");
    message = message.replace(re, vowelList[vowel]);
  }
  return(message);
}

function decodeMessage(message) {
  var chars = message.split('');
  for(let i = 0; i < chars.length; i++) {
    if(chars[i] === "0")  message = message.replace(chars[i],"o");
    if(chars[i] === "1")  message = message.replace(chars[i],"i");
    if(chars[i] === "2")  message = message.replace(chars[i],"u");
    if(chars[i] === "3")  message = message.replace(chars[i],"e");
    if(chars[i] === "4")  message = message.replace(chars[i],"a");    
    if(chars[i] === "5")  message = message.replace(chars[i],"y");
  }
  return message;
}

function decodeMessage(){
  return message
  .replace(/0/g,'o')
  .replace(/1/g,'i')
  .replace(/2/g,'u')
  .replace(/3/g,'e')
  .replace(/4/g,'a')
  .replace(/5/g,'y');
}

function decodeMessage(message) {
  for(let i = 0; i < message.length; i++) {
    if(!isNaN(parseInt(message.charAt(i)))){
      if(message.charAt(i) === '0') message.replaceAt(i,'o');
      else if(message.charAt(i) === '1') message.replaceAt(i,'i');
      else if(message.charAt(i) === '2') message.replaceAt(i,'u');
      else if(message.charAt(i) === '3') message.replaceAt(i,'e');
      else if(message.charAt(i) === '4') message.replaceAt(i,'a');
      else if(message.charAt(i) === '5') message.replaceAt(i,'y');
    } 
  }
  return message;
}

function decodeMessage(message) {
  var str = message.split('');
  for (var i = 0; i < str.length; i++) {
    if ( str[i] === "0" ) { 
      str[i] = "o";
    } else if ( str[i] === "1" ) { 
      str[i] = "i";
    } else if ( str[i] === "2" ) { 
      str[i] = "u";
    } else if ( str[i] === "3" ) { 
      str[i] = "e";
    } else if ( str[i] === "4" ) { 
      str[i] = "a";
    } else if ( str[i] === "5" ) { 
      str[i] = "y";
    }
  }
   
  return str.join('');
}

function decodeMessage(message) {
  var msg = message;
  msg = msg.split("0").join("o")
  .split("1").join("i")
  .split("2").join("u")
  .split("3").join("e")
  .split("4").join("a")
  .split("5").join("y");
  return msg;
}
3 Likes
#4

It’s been fun :clap:. Here’s mine:

const powerOn = () => ship.powerOn = true;
const countModules = () => availableModules.length;
const countEssential = () => (availableModules.filter(m => m.essential)).length;

const loadModule = (index) => {
  availableModules[index].enabled = true;
  ship.modules.push(availableModules[index]);
};

const findModuleIndex = (name) => {
  for(let i = 0; i < availableModules.length; i++) {
    if(availableModules[i].name === name){
      return i;
    }
  }
};

const resetLARRY = () => {
  for(i = 0; i < 10; i++){
    LARRY.quack();
  }
}

const moduleArr = ['life-support', 'propulsion', 'navigation', 'communication'];
moduleArr.map(a => loadModule(findModuleIndex(a)));

const setMessage = () => radio.message = JSON.stringify(navigation);
//setMessage();

const activateBeacon = () => radio.beacon = true;
//activateBeacon();

const setFrequency = () => radio.frequency = (radio.range.low + radio.range.high) / 2;

const coordsArr = ['x','y','z'];
const initialize = () => coordsArr.map(a => navigation[a] = 0);

const calibrateCoordinate = (coordinate, cycles) => {
  let signal;
  for(let i = 0; i < cycles; i++){
    signal = checkSignal();
    if(signal !== undefined){
      navigation[coord] = signal;
      break;
    }
  }
}

const calibrate = () => [['x', 12], ['y', 60], ['z', 60]].map(a => calibrateCoordinate(a[0], a[1]));

const setSpeed = speed => {if(parseInt(speed) >= 0) navigation.speed = parseInt(speed)};

const activateAntenna = () => ship.antenna.active = true;
//activateAntenna();

const sendBroadcast = () => {
  for(let i = 0; i < 100; i++){
    broadcast();
  }
};

const configureBroadcast = () => {
  setFrequency();
  activateAntenna();
  sendBroadcast();
};
//configureBroadcast();

const vowels = {'0': 'o', '1': 'i', '2': 'u', '3': 'e', '4': 'a', '5': 'y'};
const decodeMessage = (message) => message.replace(/\d/g, v => vowels[v]);

const returnToEarth = () => coordsArr.map(c -> navigation[c] = parseInt(decodeMessage(broadcast(c)), 16));
//returnToEarth();
4 Likes
#5

Thank you for the challenge ! Here’s mine:

function powerOn(){
  ship.powerOn = true;
}
function countModules(){
  return availableModules.length;
}
function countEssential(){
  var essentialModules = 0;
  for(var i = 0; i < availableModules.length; i++){
     if(availableModules[i].essential === true){
       essentialModules++;
     }
  }
  return essentialModules;
}

function findModuleIndex(name, essential){
  for(var i = 0; i < availableModules.length; i++){
  
    if(availableModules[i].name == name){ if(essential){
        if(availableModules[i].essential === true){
          return i;
        }else {
          return false;
        }
      }
      return i;
    }
  }
  return false;
}

function loadModule(index){
  if(index > -1 && index !== false){
    availableModules[index].enabled = true;
    ship.modules.push(availableModules[index]);
  }
  return false;
}

loadModule(findModuleIndex('life-support', true));
loadModule(findModuleIndex('propulsion', true));
loadModule(findModuleIndex('navigation', true));

function resetLARRY(){
  for(let i = 0; i < 10; i++){
    LARRY.quack();
  }
}
resetLARRY();

loadModule(findModuleIndex('communication', false));

function setMessage(message){
  radio.message = JSON.stringify(message);
}
setMessage(navigation);

function activateBeacon(){
  radio.beacon = true;
}
activateBeacon();

function setFrequency(){
  radio.frequency = (radio.range.low + radio.range.high)/2;
}

function initialize(){
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
}

function calibrateCoordinate(coordinate, rangeMin, rangeMax){
  let signal;
  for(let i = rangeMin; i < rangeMax; i++){
    signal = checkSignal()
    if(signal !== undefined){
      navigation[coordinate] = signal
      return navigation[coordinate];
    }
  }
  return false;
}

function calibrate(){
  calibrateCoordinate('x', 1, 12);
  calibrateCoordinate('y', 1, 60);
  calibrateCoordinate('z', 1, 60);
}

function setSpeed(speed){
  let intSpeed = parseInt(speed,10);
  if(intSpeed >= 0){
    navigation.speed = intSpeed;
  }
}
 
function configureBroadcast(){
  setFrequency()
  ship.antenna.active = true;
  for(let i = 0; i < 100; i++){
    broadcast();
  }
}
configureBroadcast();

function replaceLetter(letter) {
  let refLetters = { "0":"o", "1":"i", "2":"u", "3":"e", "4":"a", "5":"y" };
  return refLetters[letter];
}

function decodeMessage(message){
  let messageSplit = message.split('');
  for(let i=0; i < messageSplit.length; i++){
  	let newLetter = replaceLetter(messageSplit[i]);
  	if(newLetter !== undefined){
  		messageSplit[i] = newLetter;
  	}
  }
  return messageSplit.join('');
}
function returnToEarth(){
  let x = broadcast('x');
  let y = broadcast('y');
  let z = broadcast('z');
  
  navigation.x = parseInt(decodeMessage(x),16);
  navigation.y = parseInt(decodeMessage(y),16);
  navigation.z = parseInt(decodeMessage(z),16);
}
returnToEarth();
#6

Thank you @cetteSara, thank you @jbberinger, I was quite eager to see both of your codes :slight_smile:

2 Likes
#7
// Challenge 1
var powerOn = () => ship.powerOn = true;

// Challenge 2
var countModules = () => availableModules.length;

// Challenge 3
var countEssential = () => availableModules.filter(m => m.essential).length;

// Challenge 4
var loadModule = index => {
  availableModules[index].enabled = true;
  ship.modules.push(availableModules[index]);
};
var findAvailableModulesBy = (k, v) => availableModules.findIndex(m => m[k] == v);
var lifeSupportIndex = findAvailableModulesBy('name', 'life-support');
loadModule(lifeSupportIndex);

// Challenge 5
var findModuleIndex = (name) => findAvailableModulesBy('name', name);
var propulsionIndex = findModuleIndex('propulsion');
loadModule(propulsionIndex);

// Challenge 6
var navigationIndex = findModuleIndex('navigation');
loadModule(navigationIndex);

// Challenge 7
var resetLARRY = () => {
  for (var i = 0; i < 10; ++i) {
    LARRY.quack();
  }
};
resetLARRY();

// Challenge 8
var communicationIndex = findModuleIndex('communication');
loadModule(navigationIndex);

// Challenge 9
var setMessage = () => {
  const navigationStatus = JSON.stringify(navigation);
  radio.message = navigationStatus;
};
setMessage();

// Challenge 10
var activateBeacon = () => radio.beacon = true;

// Challenge 11
var setFrequency = () => radio.frequency = (
  radio.range.low + radio.range.high
) / 2.0;

// Challenge 12
var initialize = () => navigation.x = navigation.y = navigation.z = 0;

// Challenge 13
var calibrateX = () => calibrateDim(12, 'x');

// Challenge 14
var calibrateDim = (n, dim) => {
  for (var i = 1; i <= n; ++i) {
    var signal = checkSignal();
    if (signal !== undefined) {
      navigation[dim] = signal;
      break;
    }
  }
};
var calibrateY = () => calibrateDim(60, 'y');
var calibrateZ = () => calibrateDim(60, 'z');

// Challenge 15
var calibrate = () => {
  calibrateX();
  calibrateY();
  calibrateZ();
};

// Challenge 16
var setSpeed = (speed) => {
  const newSpeed = parseInt(speed);
  navigation.speed = newSpeed >= 0 ? newSpeed : navigation.speed;
};

// Challenge 17
var activateAntenna = () => ship.antenna.active = true;
// activateAntenna();

// Challenge 18
var sendBroadcast = () => {
  for (var i = 0; i < 100; ++i) {
    broadcast();
  }
};
// sendBroadcast();

// Challenge 19
var configureBroadcast = () => {
  setFrequency();
  activateAntenna();
  sendBroadcast();
};
configureBroadcast();

// Challenge 20
var decodeMessage = (message) => {
  return message
    .replace(/4/g, 'a')
    .replace(/3/g, 'e')
    .replace(/1/g, 'i')
    .replace(/0/g, 'o')
    .replace(/2/g, 'u')
    .replace(/5/g, 'y');
};

// Challenge 21
var returnToEarth = () => {
  const messages = {
    x: broadcast('x'),
    y: broadcast('y'),
    z: broadcast('z')
  };
  const decodedMessages = {
    x: decodeMessage(messages.x),
    y: decodeMessage(messages.y),
    z: decodeMessage(messages.z)
  };
  navigation.x = parseInt(decodedMessages.x, 16);
  navigation.y = parseInt(decodedMessages.y, 16);
  navigation.z = parseInt(decodedMessages.z, 16);
};
returnToEarth();
1 Like
#8

Sorry chads but @jbberinger as badass code overhere !
(Im takin notes :eyes: ! )

2 Likes
#9

… Wow ~ :heart_eyes:

2 Likes
#10
const powerOn = () => {
 if(!ship.powerOn){
   ship.powerOn = true;
 }
};

powerOn();

const countModules = () => {
 return availableModules.length; 
}; 

/*name,size,enabled,essential*/

const countEssential = () => {
 count = 0;
 availableModules.forEach((obj) =>{
  if(obj.essential) count++;
 });
 return count;
};

const findModuleIndex = (moduleName) => {
 let moduleIndex;
 availableModules.forEach((obj,index)=> {
   if(obj.name === moduleName && obj.hasOwnProperty(`essential`)) moduleIndex = index;
 });
 return moduleIndex;
};

const lifeSupportIndex = findModuleIndex('life-support');
const propulsionIndex = findModuleIndex('propulsion');
const navigationIndex = findModuleIndex(`navigation`);
const communicationIndex = findModuleIndex(`communication`);

const loadModule = (index) => {
 availableModules[index].enabled = true;
 ship.modules = [...ship.modules,availableModules[index]];
};

loadModule(lifeSupportIndex);
loadModule(propulsionIndex);
loadModule(navigationIndex);

const resetLARRY = () => {
 for (let x = 0; x < 10 ; x++){
   LARRY.quack();
 }
};
resetLARRY();

loadModule(communicationIndex);

const setMessage = (obj) => {
 radio.message = JSON.stringify(obj);
};
setMessage(navigation);

const activateBeacon = () => {
 radio.beacon = true;
};
activateBeacon();

const setFrequency = () => radio.frequency = (radio.range.low + radio.range.high )/2;

const initialize = () => {
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
};


const calibrateAxis = (axis,checks) => {
 for(let i = 0 ; i < checks ; i ++){
   let signal = checkSignal();
   if(signal){
     axis = signal;
     break;
   }
 }
};

const calibrateX = () => calibrateAxis(navigation.x,12);
const calibrateY = () => calibrateAxis(navigation.y,60);
const calibrateZ = () => calibrateAxis(navigation.z,60);


const calibrate = () => {
 calibrateX();
 calibrateY();
 calibrateZ();
};

const setSpeed = (speed) => {
 let speedNum = parseInt(speed);
 if( speedNum >= 0 ) navigation.speed = speedNum;
};

const activateAntenna = () => {
 ship.antenna.active = true;
};


const sendBroadcast = () => {
 for(let i = 0 ; i < 100 ; i ++){
   broadcast();
 }
};


const configureBroadcast = () => {
 setFrequency();
 activateAntenna();
 sendBroadcast();
};
configureBroadcast();

const decodeMessage = (message) => {
 const vowels = ['o','i','u','e','a','y'];
 let decoded = message;
 for(let i = 0 ; i < vowels.length ; i ++ ){
   let reg = new RegExp(`${i}`,'g');
   decoded = decoded.replace(reg,vowels[i]);
 }
 return decoded;
};

const returnToEarth = () => {
 let CordsX = broadcast('x');
 let CordsY = broadcast('y');
 let CordsZ = broadcast('z');
 
 let hexX = decodeMessage(CordsX);
 let hexY = decodeMessage(CordsY);
 let hexZ = decodeMessage(CordsZ);
 
 navigation.x = parseInt(hexX , 16);
 navigation.y = parseInt(hexY , 16);
 navigation.z = parseInt(hexZ , 16);
 
};

returnToEarth();
2 Likes
#11

My goal was to write every function as a single expression :laughing:

const powerOn = () => ship.powerOn = true
const countModules = () => availableModules.length
const countEssential = () => availableModules.filter(x => x.essential).length
const loadModule = index => (
  availableModules[index].enabled = true,
  ship.modules.push(availableModules[index])
)
const findModuleIndex = nm => (
  availableModules.findIndex(({essential, name}) => essential && name === nm)
)
const loadModuleByName = nm => loadModule(findModuleIndex(nm))
const resetLARRY = () => [...Array(10)].forEach(_ => LARRY.quack())
const setMessage = () => radio.message = JSON.stringify(navigation)
const activateBeacon = () => radio.beacon = true
const setFrequency = () => radio.frequency = (
  (radio.range.low + radio.range.high) / 2
)
const initialize = () => navigation.x = navigation.y = navigation.z = 0
const calib = axis => () => (
  times = axis === 'x' ? 12 : 60,
  [...Array(times)].forEach(() => (
    signal = checkSignal(),
    signal && (navigation[axis] = signal))
  )
)
const calibrateX = calib('x')
const calibrateY = calib('y')
const calibrateZ = calib('z')
const calibrate = () => calibrateX() || calibrateY() || calibrateZ()
const setSpeed = speed => speed >= 0 && (navigation.speed = parseInt(speed))
const activateAntenna = () => ship.antenna.active = true
const sendBroadcast = () => [...Array(100)].forEach(broadcast)
const configureBroadcast = () => (
  setFrequency(),
  activateAntenna(),
  sendBroadcast()
)
const decodeLetter = letter => (
  letter === '0' ? 'o' :
  letter === '1' ? 'i' :
  letter === '2' ? 'u' :
  letter === '3' ? 'e' :
  letter === '4' ? 'a' :
  letter === '5' ? 'y' :
  letter
)
const decodeMessage = message => message.split('').map(decodeLetter).join('')
const returnToEarth = () => (
  [newX, newY, newZ] = ['x', 'y', 'z']
    .map(broadcast)
    .map(decodeMessage)
    .map(hex => parseInt(hex, 16)),
  navigation = {
    ...navigation,
    x: newX,
    y: newY,
    z: newZ
  }
)

// loadModuleByName('life-support')
// loadModuleByName('propulsion')
// loadModuleByName('navigation')
// resetLARRY()
// loadModuleByName('communication')
// setMessage()
// activateAntenna()
// sendBroadcast()
// configureBroadcast()
returnToEarth()
2 Likes
#12

Mine is a junior version of everyone else’s complex versions ahahhahaha

function powerOn(){
  ship.powerOn=true;
}
function countModules(){
return availableModules.length;
}
function countEssential(){
  var count=0;
for (let i = 0; i < availableModules.length; i++){
  if (availableModules[i].essential===true){
 count=count+1;
 }
 }
return count;
}  
function loadModule(index){
availableModules[index].enabled=true;
ship.modules.push(availableModules[index]);
}
function findModuleIndex(name) {
  for ( let i = 0; i < availableModules.length; i++) {
    if (availableModules[i].name==name && availableModules[i].essential ===true){
      return i;
    }
  }
}
loadModule(findModuleIndex("life-support"));
loadModule(findModuleIndex("propulsion"));
loadModule(findModuleIndex("navigation"));
loadModule(findModuleIndex("communication"));

function activateBeacon(){
  radio.beacon=true;
}
activateBeacon();

function setFrequency(){
  radio.frequency=((radio.range.high+radio.range.low)/2);
}
function initialize(){
  navigation.x=0;
  navigation.y=0;
  navigation.z=0;
}
function calibrateX(){
for (let i=1; i<=12;i++){
  var signal=checkSignal();
    if (typeof signal!=='undefined'){
      navigation.x=signal;
  break;
    }
  }
}
function calibrateY(){
for (let i=1; i<=60;i++){
  var signal=checkSignal();
    if (typeof signal!=='undefined'){
      navigation.y=signal;
  break;
    }
  }
}
function calibrateZ(){
for (let i=1; i<=60;i++){
  var signal=checkSignal();
    if (typeof signal!=='undefined'){
      navigation.z=signal;
  break;
    }
  }
}
function calibrate(){
  calibrateX();
  calibrateY();
  calibrateZ();
}
function setSpeed(speed){
  var i=parseInt(speed,10);
  if (i>=0){
  navigation.speed=i;
  }
}
function activateAntenna(){
  ship.antenna.active=true;
}
function sendBroadcast(){
  for (let i=1;i<=100;i++){
    broadcast();
  }
}
function configureBroadcast(){
 setFrequency();
 activateAntenna();
 sendBroadcast();
}
configureBroadcast();
function decodeMessage(message){
  message=message
  .replace(/1/g,'i')
  .replace(/2/g,'u')
  .replace(/3/g,'e')
  .replace(/4/g,'a')
  .replace(/5/g,'y')
  .replace(/0/g,'o');
  return message;
}
function returnToEarth(){
  var X=broadcast("x");
  var Y=broadcast("y");
  var Z=broadcast("z");
  navigation.x=parseInt(decodeMessage(X),16);
  navigation.y=parseInt(decodeMessage(Y),16);
  navigation.z=parseInt(decodeMessage(Z),16);
}
returnToEarth();
2 Likes
#13

Since challenge one you went to :100: real quick! :eyes:

Just saying :stuck_out_tongue: !~

2 Likes
#14

Here is mine

with some documentation :slight_smile:

/**
 Ship power on function 
 */
let powerOn = () => {
    if (!ship.powerOn) ship.powerOn = true;
};
/**
 * @return the number of modules that can be used
 */
let countModules = () => {
    return availableModules.length;
};

/**
 * filters the essential modules using boolean test function based on Array.filter prototype
 * @return number
 */
let countEssential = () => {
    return availableModules.filter(function(module) {
        return module.essential;
    }).length;
};
/**
 * Loads specified module whether it's essential or not
 * @param index - The module index in array
 * @param essential - essential flag value (optional)
 */
let loadModule = (index, essential) => {
    //Its good practice to do some parameters assertions 
    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);
    //Get the module that is at specified index and enable it
    let module = availableModules[index];
    module.enabled = true;
    // check if essential parameter was provided, is so, set the module property to it 
    if (essential !== undefined) module.essential = essential;
    ship.modules.push(module);
};

let findModuleIndex = (moduleName) => {
    return availableModules.findIndex(function(module) {
        return module.name == moduleName;
    });
};

/**
 * Encapsulate the module loading process
 * @param name - What module to load
 * @param essential - flag that determine if module is absolutely required
 */
let loadModuleByName = (name, essential) => {
    loadModule(findModuleIndex(name, essential));
};
/** reset Larry by making it quack 10 time 
 *
 */
let resetLARRY = () => {
    for (let i = 0; i < 10; i++) LARRY.quack();
    /*
     if performance was an issue in context, we could have done without the for loop like so:
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack();
     LARRY.quack(); <- The 10th call
    */
};
/**
 * set the radio message for later broadcast
 * @param message - the message to set the radio to
 */
let setMessage = (message) => {
    radio.message = message;
};
/**
 * Activate the radio beacon prior to any broadcast 
 */
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;
        }
    }
};
let calibrateY = () => {
    for (let i = 1; i <= 60; i++) {
        let signal = checkSignal();
        if (signal !== undefined) {
            navigation.y = signal;
            break;
        }
    }
};
let calibrateZ = () => {
    for (let i = 1; i <= 60; i++) {
        let signal = checkSignal();
        if (signal !== undefined) {
            navigation.z = signal;
            break;
        }
    }
};

let calibrate = () => {
    calibrateX();
    calibrateY();
    calibrateZ();
};

let setSpeed = (speed) => {
    if (!isNaN(speed)) {
        let lSpeed = parseInt(speed);
        if (lSpeed >= 0) navigation.speed = lSpeed;
    }
};

let activateAntenna = () => {
    ship.antenna.active = true;
};
let sendBroadcast = () => {
    let i = 0;
    while (i < 100) {
        broadcast();
        i++;
    }
};
let configureBroadcast = () => {
    setFrequency();
    activateAntenna();
    sendBroadcast();
};
let decodeMessage = (message) => {
    message = message.replace(/0/g, "o");
    message = message.replace(/1/g, "i");
    message = message.replace(/2/g, "u");
    message = message.replace(/3/g, "e");
    message = message.replace(/4/g, "a");
    message = message.replace(/5/g, "y");
    return message;
}
let returnToEarth = () => {
    let x = broadcast("x");
    let y = broadcast("y");
    let z = broadcast("z");
    navigation.x = parseInt(decodeMessage(x), 16);
    navigation.y = parseInt(decodeMessage(y), 16);
    navigation.z = parseInt(decodeMessage(z), 16);
}
loadModuleByName("life-support");
loadModuleByName("propulsion", true);
loadModuleByName("navigation", true);
loadModuleByName("communication", true);
resetLARRY();
setMessage(JSON.stringify(navigation));
activateBeacon();
configureBroadcast();
returnToEarth();
1 Like
#15
function powerOn() {
  ship.powerOn = true;
}

function countModules() {
  return availableModules.length;
}

function countEssential() {
  return availableModules.filter(x => x.essential).length;
}

function loadModule(index) {
  let module = availableModules[index];
  module.enabled = true;
  ship.modules.push(module);
}

function findModuleIndex(name) {
  return availableModules.map(x => x.name).indexOf(name);
}

function resetLARRY() {
  Array(10).fill().forEach(x => LARRY.quack());
}

function setMessage() {
  radio.message = JSON.stringify(navigation);
}

function activateBeacon() {
  radio.beacon = true
}

function setFrequency() {
  let low = radio.range.low;
  let high = radio.range.high;
  radio.frequency = 0.5 * (low + high);
}

function initialize() {
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
}

function getSignal(max) {
  for (let i = 0; i < max; i++) {
    signal = checkSignal();
    if (signal !== undefined) {
      return signal;
    }
  }
}

function calibrateX() {
  navigation.x = getSignal(12);
}

function calibrateY() {
  navigation.y = getSignal(60);
}

function calibrateZ() {
  navigation.z = getSignal(60);
}

function calibrate() {
  calibrateX();
  calibrateY();
  calibrateZ();
}

function setSpeed(speed) {
  let s = parseInt(speed, 10);
  if (s >= 0) {
    navigation.speed = Math.sign(s) * s;
  }
}

function activateAntenna(){
  ship.antenna.active = true
}

function sendBroadcast() {
  Array(100).fill().forEach(() => broadcast());
}

function configureBroadcast() {
  setFrequency();
  activateAntenna();
  sendBroadcast();
}

function decodeMessage(message) {
  const chars = [['0', 'o'], ['1', 'i'], ['2', 'u'],
                 ['3', 'e'], ['4', 'a'], ['5', 'y']];
  return chars.reduce((acc, x) => acc.split(x[0]).join(x[1]), message);
}

function returnToEarth() {
  ["x", "y", "z"].forEach(x => navigation[x] = parseInt(decodeMessage(broadcast(x)), 16));
}

loadModule(findModuleIndex("life-support"));
loadModule(findModuleIndex("propulsion"));
loadModule(findModuleIndex("navigation"));
loadModule(findModuleIndex("communication"));
resetLARRY();
setMessage();
activateBeacon();
configureBroadcast();
returnToEarth();

Was fun this May! Tried to be a bit creative :slight_smile:

2 Likes
#16

Feel like most of my code wouldn’t be any different so I’m just sharing my decodeMessage function.

 function decodeMessage(message){
  var msgArr = message.split('');
  var decodeArr = ['o', 'i', 'u', 'e', 'a', 'y'];
  
  for(var i = 0; i < msgArr.length; i++){
    var msgNum = parseInt(msgArr[i]);
    if(msgNum >= 0){
      msgArr[i] = decodeArr[msgNum];
    }
  }
  
  message = msgArr.join('');
  return message;
}
1 Like
#17

Here are my 2 cents! Nice solutions all around! :slight_smile:


function decodeMessage(message){
  return message.replace(/[0-9]/g, function(str){ 
    switch(str){
      case "0":
      return "o";
      break;
      case "1":
      return "i";
      break;
      case "2":
      return "u";
      break;
      case "3":
      return "e";
      break;
      case "4":
      return "a";
      break;
      case "5":
      return "y";
      break;
  }
});
}

function returnToEarth(){
  
  navigation.x = parseInt(decodeMessage(broadcast("x")) , 16);
  navigation.y = parseInt(decodeMessage(broadcast("y")) , 16);
  navigation.z = parseInt(decodeMessage(broadcast("z")) , 16);
  
}

returnToEarth();
#18

Hoo that’s nice. Thank you :slight_smile:

#19

Very similar to the solutions by @cbochs

function powerOn() {
  ship.powerOn = true;
}

function countModules(){
  return availableModules.length;
}

function countEssential(){
  return availableModules.filter((x) => x.essential === true).length;
}

function findModuleIndex(moduleName){
  return availableModules.indexOf(availableModules.find((x) => x.name == moduleName));
}

function loadModule(index){
  var m = availableModules[index];
  m.enabled = true;
  ship.modules.push(m);
}

function resetLARRY(){
  for (var i=0; i < 10; i++){
    LARRY.quack();
  }
}

function setMessage(message){
  radio.message = JSON.stringify(message);
}

function activateBeacon() {
  radio.beacon = true;
}

function setFrequency() {
  radio.frequency = ( radio.range.low + radio.range.high ) / 2;
}

function initialize(){
  navigation.x = 0;
  navigation.y = 0;
  navigation.z = 0;
}

function calibrateAxis( axis, min, max ) {
  for (var i = min; i <= max ; i++ ) {
    if ( typeof ( signal = checkSignal() ) !== 'undefined' ) {
      navigation[axis] = signal;
      break;
    }
  }
}

function calibrateX() {
  calibrateAxis( 'x' , 1 , 12);
}

function calibrateY() {
  calibrateAxis( 'y' , 1 , 60 );
}

function calibrateZ() {
  calibrateAxis( 'z' , 1 , 60 );
}

function calibrate() { calibrateX(); calibrateY(); calibrateZ(); }

function setSpeed( speed ) {
  navigation.speed = (parseInt( speed, 10 ) >= 0) ? parseInt( speed, 10 ) : navigation.speed;
}

function activateAntenna() {
  ship.antenna.active = true;
}

function sendBroadcast() {
  for (var i = 0; i < 100; i++) { broadcast(); }
}

function configureBroadcast() {
  setFrequency();
  activateAntenna();
  sendBroadcast();
}

function decodeMessage(message) {
	return message.split('').reduce((m, l) => m += { '4' : 'a', '3' : 'e', '1' : 'i', '0' : 'o', '2' : 'u', '5' : 'y' }[l] || l , '' );
}

function returnToEarth() {
  'xyz'.split('').map( c => navigation[c] = parseInt( decodeMessage( broadcast( c ) ), 16 ) );
}


loadModule(findModuleIndex("life-support"));
loadModule(findModuleIndex("propulsion"));
loadModule(findModuleIndex("navigation"));

resetLARRY();

setMessage(navigation);

loadModule(findModuleIndex("communication"));

configureBroadcast();

returnToEarth();
1 Like