21 Day Coding Challenge

Challenge 21 - stuck in space

#1

Here’s mine and it’s not getting me back to earth. At this rate I may orbit forever. Can anyone suggest what’s wrong?

function returnToEarth(){

let res1 = broadcast(‘x’);
let res2 = broadcast(‘y’);
let res3 = broadcast(‘z’);

let dm1 = decodeMessage(res1);
let dm2 = decodeMessage(res2);
let dm3 = decodeMessage(res3);

navigation.x = parseInt(‘dm1’, 16);
navigation.y = parseInt(‘dm2’, 16);
navigation.z = parseInt(‘dm3’, 16);
}

returnToEarth();

#2
function returnToEarth(){

let res1 = broadcast(‘x’);
let res2 = broadcast(‘y’);
let res3 = broadcast(‘z’);

let dm1 = decodeMessage(res1);
let dm2 = decodeMessage(res2);
let dm3 = decodeMessage(res3);

navigation.x = parseInt(‘dm1’, 16);
navigation.y = parseInt(‘dm2’, 16);
navigation.z = parseInt(‘dm3’, 16);
}

returnToEarth();

You’re return statement is outside of your function. But also, and more importantly, you do not need a return statement for this function at all.

Note, I copied your code and placed it inside 3 backticks for it to show up like this, in case you were wondering.

Edit: you do need to call your function. Hopefully you did that at the bottom of your code.
Edit 2: misunderstood your code the first time. I crossed out my mistake.

#3

Thanks for taking a look at this. However, I’m still getting ‘invalid or unexpected token’ with this format. :confused:

#4

Try removing the quotes from :

navigation.x = parseInt(‘dm1’, 16);

to this instead:

navigation.x = parseInt(dm1, 16);
#5

Yup - tried that too. Think I need to walk away from it for a bit. BTW the first place in the code which throws up a warning is
//let dm3 = decodeMessage(res3);//

#6

I’m not sure what the rest of your code looks like, but do you have your returnToEarth(); statement at the very end of your code?

#7

I would say to remove the quotes as well :
parseInt(dm1, 16);
// As dm1 is a variable, and not a string (well, the content of the variable is a string, but here the parameter is your variable dm1).

Honestly, if this doesn’t work, I’m lost. The rest of your code looks a lot like mine and it worked for me. The only thing I did differently is that I didn’t store the result of the decodeMessage() function in another variable.
And then in the parseInt, I wrote :
parseInt(decodeMessage(//my variable storing broacast (‘x’)//), 16);

Hope it helps somehow!

#8

Still in orbit. I’ve taken advice on board and come up with this which doesn’t throw the ‘unexpected token’ error and runs but doesn’t produce the correct result apparently.

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();

#9

Again, is your call at the bottom of your code?

Here is what my call functions look like in my local testing file:

All my functions themselves are above all the calls. Is that the case in your code?

#10

Two things…

Your code seems ok… So let’s think outside the box…

  1. did you declare the navigation variable globally somewhere in your code?

  2. could you show us decodeMessage()?

Most likely it’s something else interfering with it…

#11

I did this in Challenge 12:
function initialize(){
navigation.x = 0;
navigation.y = 0;
navigation.z = 0;

but it’s not global.

Here’s my decodeMessage function:

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

return message;
}

and this was my original solution for Challenge 21:

function returnToEarth(){
let res1 = broadcast(‘x’);
let res2 = broadcast(‘y’);
let res3 = broadcast(‘z’);

let dm1 = decodeMessage(res1);
let dm2 = decodeMessage(res2);
let dm3 = decodeMessage(res3);

navigation.x = parseInt(dm1, 16);
navigation.y = parseInt(dm2, 16);
navigation.z = parseInt(dm3, 16);
}
returnToEarth();

which produced an ‘unexpected token’ error.

#12

You could DM us your whole code and we can provide feedback/hints on this thread.

1 Like
#13

I second what Fred just said, sorry for the inconvenience, but there doesn’t seem to be anything wrong with your code so far… It must be hidden somewhere else…

#14

Think I’ve figured out the DM. Will send my whole code. Thanks for your patience.

#15

I’m losing it with the DM. Here’s my whole code:

//CHALLENGE 1//
function powerOn(){
ship.powerOn = true;
}

//CHALLENGE 2//
function countModules(){
return availableModules.length;
}

//CHALLENGE 3//
function countEssential(){
var count = 0;
for(let i = 0; i < availableModules.length; i++){

if (availableModules[i].essential===true) {

  count = count + 1;

}
}
return count;

}

//CHALLENGE 4//

function loadModule(index) {

availableModules[index].enabled === true;
ship.modules.push(availableModules[index]);

}
for(let i = 0; i < availableModules.length; i++) {
if(availableModules[i].name === “life-support”){
loadModule(i);
}

}

//CHALLENGE 5//

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].essential && availableModules[i].name === name) {
return i;
}
}
}
loadModule(findModuleIndex(“propulsion”));

//CHALLENGE 6//

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].essential && availableModules[i].name === name) {
return i;
}
}
}
loadModule(findModuleIndex(“navigation”));

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

}

}
resetLARRY();

//CHALLENGE 8//
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].essential && availableModules[i].name === name) {
return i;
}
}
}
loadModule(findModuleIndex(“communication”));

//CHALLENGE 9//

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

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

//CHALLENGE 11//

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

}

//CHALLENGE 12//

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

}

//CHALLENGE 13//

function calibrateX(){

for(let i=0; i <=12; i++)
{

let signal = checkSignal();
if (signal !== undefined)
{
  navigation.x = signal;

   break;
}

}
}
//calibrateX();//

function calibrateY()
{
for (let i = 0; i<= 60; i++)
{
let signal = checkSignal();
if (signal !== undefined)
{
navigation.y = signal;

  break;
}

}
}
//calibrateY()//;

function calibrateZ()
{
for (let i = 0; i <= 60; i++)
{
let signal = checkSignal();
if (signal !== undefined)
{
navigation.z = signal;

  break;
}

}
}
//calibrateZ();//

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

//CHALLENGE 16//

function setSpeed(speed){

if (speed >= 0){

navigation.speed = parseInt(speed, 10);

}
}
//CHALLENGE 17//

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

//activateAntenna();//

//CHALLENGE 18//
function sendBroadcast(){
for(let i = 0; i < 100; i++){
broadcast();
}
}
//sendBroadcast();//

//CHALLENGE 19//

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

}
configureBroadcast();

//CHALLENGE 20//
function decodeMessage(message){
message = message
.replace(/1/g, ‘i’)
.replace(/4/g, ‘a’)
.replace(/3/g, ‘e’)
.replace(/0/g, ‘o’)
.replace(/5/g, ‘y’)
.replace(/2/g, ‘u’);

return message;
}

//CHALLENGE 21//

function returnToEarth(){

let res1 = broadcast(‘x’);
let res2 = broadcast(‘y’);
let res3 = broadcast(‘z’);

let dm1 = decodeMessage(res1);
let dm2 = decodeMessage(res2);
let dm3 = decodeMessage(res3);

navigation.x = parseInt(dm1, 16);
navigation.y = parseInt(dm2, 16);
navigation.z = parseInt(dm3, 16);
}
returnToEarth();

#16

In my testing environment, I tried moving all your call functions, in order, to the bottom of the code and it seemed to fix the issue. Try that:

loadModule(findModuleIndex('communication'));
loadModule(findModuleIndex('propulsion'));
loadModule(findModuleIndex('navigation'));
resetLARRY();
configureBroadcast();
returnToEarth();

Go find all those calls, and bring them down to the bottom in this order.

#17

I haven’t finish reviewing everything (Sorry for the delay, my computer was updating), but I don’t see a 1, in this function…

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

  return message;
}
#18

By ‘the bottom of the code’, do you mean inside or outside the final curly brace?

#19

This line also has a problem, though it’s unlikely the cause of your issue:

availableModules[index].enabled === true;

You’ll only use the === to test a value, but in this case, you’re trying to assign it…

still looking at the code

#20

It’s there in my version:

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

return message;
}