21 Day Coding Challenge

Challenge 5 - challenging me

#1

What is the matter with this code ?

It goes trough all the modules, matches the specification, but apparently , that is not the answer.
I do call both fucntions at the end, but it still does not work

//day 5

function findModuleIndex(sentname){     
  for(let i = 0; i < availableModules.length; i++)
  {
    if(availableModules[i].name === sentname &&
    availableModules[i].essential)
    {
      return i;
    }
  }

}

#2

I also tried…

if(availableModules[i].name === sentname &&
availableModules[i].essential)
{
index = i;
return index;
}

but no changes

#3

What is the error? I’ve tried your first code and it works fine…

Is it when you try to submit it, that you encounter an issue? Is so, did you call it like…

loadModule(findModuleIndex("propulsion"));

?

#4

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

function findModuleIndex(moduleName){
for(var i =0 ; i < availableModules.length ; i++){
if (availableModules[i].name == moduleName && availableModules[i].essential){
return i;
}
}
}

loadModule(findModuleIndex(“propulsion”));

See still doesn’t work

1 Like
#5

What is the error you get?

#6

Your code ran but did not produce the correct result. Hint: confirm your functions are properly solving the challenge.

#7

Are you still also calling loadModule for the “life-support” module?
If not, try adding that back in as well

#8

@courtneyr thanks dude. You are the man. Yes we also need to call the life-support. it worked thanks bruh!

#9

Guys, if I already had a loadModule() from chanllenge 4, why did i code a new fucntion ?
function loadModule(index){
availableModules[index].enabled = true;
ship.modules.push(availableModules[index]);
}

Now , it WORKS!

I do not understand it quite well though

#10

Hi Nathalia,
a lot of people didn’t do their loadModule() function as a standalone one, in Challenge 4. The idea of Challenge 5 was to take what you’ve learned yesterday and make sure your functions interact in a certain manner. In this case, loadModule() now only load the module based on the index it receives. Yesterday, that wasn’t a prerequisite. :slight_smile:

I hope it’s a little bit clearer… By having one block of code dedicated to finding the index, based on the string you submit, and one bit of code dedicated to loading the module, based on the index you submit to it, you now have a super-powerful combo than could load any module based on its name!!! (Well, there’s still limitations, which we’ll most likely address in the upcoming days.)

:sunny:

1 Like
#11

Where did you call life-support??

#12

At Challenge #5, what we’re trying to do is use both functions in a more versatile way… So the call for “life-support” is directly in the function call. both loadModule() and findModuleIndex() are now independent from one another, but we can use one as the argument for the other. :slight_smile:

#13

Thanks solved and on challenge 8 now :slightly_smiling_face:

#14

yes, Seb.
i am stuck now in Challenge 8 - for over a week now

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

//day 2

function countModules(){
return availableModules.length;
}

//day 3

function countEssential() {

const newArray = [];

for(var i = 0; i < availableModules.length; i++){
if(availableModules[i].essential === true){
newArray.push(1);
}
}

return newArray.length;
}

//day 4

index = 0;

  for(let i = 0; i < availableModules.length; i++ ){
    if(availableModules[i].name === "life-support"){
      availableModules[i].enabled = true;
      index = i;
    }
  }


function loadModule(index){
  ship.modules.enabled = true;
  ship.modules = availableModules.slice(index,index+1);

} 

loadModule(index);

//day 5
function loadModule(i){

if(availableModules[index].hasOwnProperty(‘essential’) ) {
availableModules[index].essential = true;
}
availableModules[i].enabled=true;
ship.modules.push(availableModules[i]);
}

function findModuleIndex(name){
for(var j=0; j<availableModules.length; j++){
if(availableModules[j].name===name
// && availableModules[index].name===name)
)
{
return j;

}

}
}

loadModule(findModuleIndex(“propulsion”));
loadModule(findModuleIndex(“life-support”));

//day 6

loadModule(findModuleIndex(“navigation”));

//day 7
function resetLARRY(){

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

//day 8

loadModule(findModuleIndex(“communication”));

#15

Hi Nathalia,
So sorry to hear that :frowning:

Let’s try to figure this out together… If I’m not mistaken, this part is what’s most likely to cause you trouble:

//day 4

index = 0;
for(let i = 0; i < availableModules.length; i++ ){
  if(availableModules[i].name === "life-support"){
    availableModules[i].enabled = true;
    index = i;
  }
}

function loadModule(index){
  ship.modules.enabled = true;
  ship.modules = availableModules.slice(index,index+1);
} 
loadModule(index);

//day 5
function loadModule(i){
  if(availableModules[index].hasOwnProperty(‘essential’) ) {
    availableModules[index].essential = true;
  }
  availableModules[i].enabled=true;
  ship.modules.push(availableModules[i]);
}

function findModuleIndex(name){
  for(var j=0; j<availableModules.length; j++) {
    if(availableModules[j].name===name ) {
    // && availableModules[index].name===name)
      return j;
    }
  }
}

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

//day 6

loadModule(findModuleIndex("navigation"));

Between day 4 and and 5, you ended up with a global variable named index, and two distinct functions called loadModule…

You most likely will need to remove anything extra… I don’t see you having a need for the day 4 loop and most likely day 5 loadModule is more “up to date”. Though at day 8, we actually update that one… Make sure you only have one function of each name :slight_smile:

Also… You might want to bring all of these together:

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

Someone said the order actually mattered, so keep that in mind. :slight_smile: All in all, there’s nothing wrong with your code, but since you have many bits of code here and there that have been updated by now code, some of it conflict with one another :slight_smile:

Please keep a copy in a text file, if you want/need to :slight_smile:

Challenge 8 - HELP PLS
Challenge 8 and Challenge Continuity
#16

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

//day 2

function countModules(){
return availableModules.length;
}

//day 3

function countEssential() {

const newArray = [];

for(var i = 0; i < availableModules.length; i++){
if(availableModules[i].essential === true){
newArray.push(1);
}
}

return newArray.length;
}

//day 4

index = 0;

//day 5

function loadModule(index){

if(availableModules[index].hasOwnProperty(‘essential’) ) {
availableModules[index].essential = true;
}
else
{

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

loadModule(index);

function findModuleIndex(moduleName){
for(var i =0 ; i < availableModules.length ; i++)
{
if (availableModules[i].name == moduleName
//&& availableModules[i].essential
)
{
return i;
}
}
}

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

resetLARRY();

loadModule(findModuleIndex(“life-support”));
loadModule(findModuleIndex(“propulsion”));
loadModule(findModuleIndex(“navigation”));
loadModule(findModuleIndex(“communication”));

#17

Nothing yet :confused:

#18

You still have 2 loadModule function and a global variable called index… :slight_smile: Those two things can definitely get in your way… It’s better to make your variables as local as possible :slight_smile:

If you want, I can “clean” your code a little and paste it back, that way you’ll see what I mean…

#19

that would be great

But previously, I had 2 functions with same name and the code passed the test still :face_with_monocle:

#20

It might have, but thing is, even if at best it would work, you can’t guarantee which of these loadModules will be called… So… Anyhow, give me a few minutes :wink:

1 Like