21 Day Coding Challenge

Challenge #3 help

#1

Hello! I have a mistake: “Your code ran but did not produce the correct result.”

function countEssential(){
var count = 0;
for(var i in availableModules){
if (availableModules[i].Essential===true) count=count+1;
}
return count;
}

Please, can you help me? Where is a problem? How I can see this “incorrect result”? If I create new blank and run this code, I see correct result.

SSSSSSS result: 1.
#2

Hi Larisa,
Often, for that one, people didn’t call their function.

I believe in Challenge #3, you need a function call.

#3

I find mistake. The letter “E” in “availableModules[i].Essential” must be small. Rigth is: “availableModules[i].essential”

1 Like
#4

function countEssential(){
var numEssential= 0;
for (i = 0; i<availableModules.length+1; i++){
if (availableModules[i].essential === true) numEssential = numEssential + 1;
}
//thisModule = [];
return numEssential;
}

I keep getting an error saying cannot read essential of undefined… help? Please + thanks!

#5

availableModules.length + 1 ?

#6

sorry I removed that as well so it’s just availableModules.length! Still having the essential of undefined issue…

1 Like
#7

I don’t see anything else wrong with it, at the moment, could be a really subtle error… Did you declare any global variables (variables that are not within your functions) in the code you’re trying to submit?

#8

Hi Tiffany,
I tested locally your code (with the slight alteration I suggested) and everything behave as should be… :frowning: Could you past everything you have in that validator, at this point? (sorry for the extra work) :pray:

#9

This is everything I have! I //Challenge 1: Create a function to turn the ship’s power on
function powerOn() {
if(ship.powerOn === false) ship.powerOn = true;
}

//Challenge 2: Create a function to count and return the number of modules in the array availbleModules AND run it.
function countModules(){
return availableModules.length;
}
countModules();

//Challenge 3: COunt the number of modules whose essential flag is set to true
function countEssential(){
var count = 0;
for(i = 0; i<availableModules.length; i++){
if (availableModules[i].essential===true){ count=count++;
}
return count;
}
}
countEssential();

//Challenge 4: Find the life-support module, enable it if its essential flag is true

//4B: Second, create the function which will iterate through all the modules in availableModules to find the one named life-support
function loopModule(){
var modName;
for (j = 0; j<availableModules.length; j++){
if (availableModules[j].name == ‘life-support’){
modName = j;
}
return modName;
}
}
// 4A: First, create the function which will load the module corresponding to position index in the availableModules array.
function loadModule(index){
//index = loopModule();
if(availableModules[index].essential===true){ //determines if the esential flag is true
availableModules[index].enabled===true,//sets the enabled flag to true
ship.modules.push(availableModules[index]);//adds the current module to the ship’s modules
}
else return “null”; // if the essential flag is not true, returns null
}

index = loopModule();
loadModule(index);

#10

Hi Tiffany, is your issue with Challenge #3 or #4?

I can see a problem with your Challenge #3 function:

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

countEssential();

return will stop the function and return a value, it’s not exactly where you’d want it…

This will also cause you problems:

function loopModule(){
  var modName;
  for (j = 0; j<availableModules.length; j++) {
    if (availableModules[j].name == 'life-support') {
      modName = j;
    }
    return modName;
  }
}

In your for loop, when will the return statement happen? Will you always have a value associated with that return statement or could you see situations where you wouldn’t?

Here:

function loadModule(index){
  //index = loopModule();
  if(availableModules[index].essential===true){ //determines if the esential flag is true
    availableModules[index].enabled===true,//sets the enabled flag to true
    ship.modules.push(availableModules[index]);//adds the current module to the ship’s modules
  }
  else return “null”; // if the essential flag is not true, returns null
}

Somewhere in your code, you try to do an assignment, but you’re trying to use === for that purpose. === can only be used for strict comparison…

Finally…

index = loopModule();
loadModule(index);

Could you think of a different way to do this than to have a global variable called index? :slight_smile:

I hope these help a little… :four_leaf_clover:

1 Like
#11

thanks a bunch! I will review this and see if they help! The main error I am still getting is that the essential flag cannot be assigned to undefined. Could I send you a message if I have further issues? Thanks

#12

Of course, please send me a message, and if there’s any extra question about what I’ve just shared, please ask :slight_smile:
:sunny:

1 Like