Codewars - simple fun #74 js - stuck

Hi coders,
I’m doing this challenge on Codewars ,but I’m stuck with second test:

For upSpeed = 10, downSpeed = 9 and desiredHeight = 4 , the output should be 1 .

Because the plant reach to the desired height at day 1(10 meters).

 After day 1 --> 10

Full text challenge --> https://www.codewars.com/kata/simple-fun-number-74-growing-plant/train/javascript

and this my code:


function growingPlant(upSpeed, downSpeed, desiredHeight) {
  //coding and coding..
  let growPlant = 0;
  
  for(var i = 0; i < desiredHeight; i++){
    growPlant = upSpeed  
      
      if(growPlant > desiredHeight){

        return  upSpeed = upSpeed + i;
        
      }
      else if (growPlant < desiredHeight){
       return downSpeed = downSpeed - i;
      }
     else{
        return i;
     }
   
  };

console.log(growingPlant(10,9,4))

This code return 10 and not 1.
Thanks for help,
CamCode

You are returning out of the loop and function. Try adding a log statement inside the loop at the top of it.

console.log('loop ran', i, 'times')

I don’t cycle like it should but I don’t know what to change in my code

  1. Do not return the result until it is ready.

  2. Use the growPlant variable to hold the number you “build-up” using upSpeed and downSpeed.

  3. Return the loop counter when growPlant is greater than or equal to the desiredHeight.

1 Like

I think I didn’t understand what you mean in your pseudocode.

  • Why shouldn’t I use the return?
  • Should I also grow plant = downSpeed?
  • why shouldn’t I use the decrease?
  1. There is only one time you want to return and that is when growPlant is greater than or equal to the desiredHeight.

  2. You need to both add and subtract to/from the growPlant value. Each “day” is one loop iteration, for each loop iteration you both grow and shrink the plant (increment and decrement the growPlant variable by the upSpeed and downSpeed). If at the start of a loop iteration growPlant has the desiredHeight you return how many times the loop ran, i.e. how many “days” it took.

function growingPlant(upSpeed, downSpeed, desiredHeight) {
  //coding and coding..
  let growPlant = 0;
  let day = 0;
  for(var i =0; i < desiredHeight; i++){
    day++;
    growPlant += upSpeed;
    if(growPlant < desiredHeight){

    growPlant -= downSpeed;

  }
  }
  
 return day; 
};

Like this?

Did you try it? You are definitely getting closer. Keep working on it.

I have tried many times but I continue with the wrong result. Now I have tried to see a video that instead of using for uses while

Just so I don’t spoil anything, did you look up a video with the answer? What you have now is really close.

Move the return into the if statement and check for the opposite of what you are (including equals). Now move the decrement below the if statement.

1 Like

Thanks now it works !!! Now I have to think about the solution you gave me and where did I go wrong

Like I said you were really close.

Sometimes you just have to take a break and walk away. Then, when you least expect it, you will think of something that gets you closer, or all the way to the solution. Get familiar with that feeling of believing that you will not find the solution. Accept that feeling as one of the steps toward finding the solution. Your brain will often prove your emotions wrong. But it does take some perseverance. Keep at it!

thanks for your support @lasjorg