21 Day Coding Challenge

Challenge 16: I'm unsure why this is not working

#1

I am unsure how to incorporate ship.modules.propulsion to get the speed. I assumed this may work, but it doesn’t complete the challenge and I’m stuck now.

edit - I was able to figure out the issue. P.S. using “if (s >= 0)” works just fine.

1 Like
#2

You need to check if your s is negative or positive,

#3

That’s what if (s >= 0) does; there’s no need to use Math.sign in this case.

1 Like
#4

Interesting, I tried with (s >= 0) and it did not work for me, Math.sign on the other hand worked flawless, this is interesting if this worked for you windzer

1 Like
#5

you need to set navigation.speed to the speed you converted to an integer :slight_smile:

3 Likes
#6

Good eye Sunny…!!!

1 Like
#7

Did it end up working?

#8

It also worked for me to use if (i >= 0) {} as my negative check, as it should. So I think maybe you changed something else when you adjusted that to get it to work?

1 Like
#9

Can anyone help me with this one? It runs fine on my javascript tester…

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

#10

The only difference that I have is that I had set s=parsInt(speed); And then I used s instead of parsInt(speed) for the rest of the function. It ran fine like that. The way you have it though looks fine to me, so don’t know why that didn’t run.

#11

Why 10 as the parseInt(“speed”, 10) ?
Where does the number 10 come from?

#12

Hoo good question :slight_smile:

It’s the base. You can pass to parseInt the base you’re working with. In this case, it’s base 10. (Which is the most common way of thinking about numbers, for human beings.)

1 Like
#13

The 10 is the base you to parse it as. It’s not required since it will parse it in base 10 by default.

#15

Right now, you’re trying to parse the variable speed as the string speed (“speed”)

parseInt(speed, 10) vs parseInt("speed", 10)
#16

Hey Windzer,

Two lines with bugs in the image you had posted

//Line 106
var s = parseInt("speed",10)
//should look something like
... parseInt(speed)  or parseInt(speed,10)

In the above, "speed" should be written as the variable, and not a string. The 10 in the second parameter is not necessary as base 10 is default. I would only recommend typing a number in there only if you are going to change the base to something else.

//Line 108
navigation.speed = ship.module.propulsion

In Line 108, we should be assigning the parsed speed value into to navigation.speed, not the value within ship.module.propulsion

#17

I am not sure if someone replied but try declaring the parsed value to a variable.
It may be helpful if you try debugging code. I guess the problem could be with the way it stacks in/run in this compiler.
Not sure if it is what makes sense. I may look stupid as well lol.
Your question is reasonable as it must work.
Also, a note that if you are to use a converted object more than once, it is better to do it once.
Let me know if this helps :slight_smile:

#18

You’ve got a good eye too. Always helping others find what went wrong with their code :slight_smile:

1 Like