0

I am trying to convert an int into a str using a if loop! here is my logiv. What is it that I am doing wrong ? Any ideas ?

var waitTime  = parseInt("53");
if (waitTime > 20) {
  return 'a lot of time'
} else if (waitTime < 20) {
  return 'we can wait'
}

console.log(waitTime);

I keep getting

1
  • You are using return statements but you have not declared or created a function here? Commented Jul 27, 2017 at 0:56

5 Answers 5

2

Your code looks mostly correct, including your usage of parseInt, but perhaps you are mixing your return statements with console.log statements?

var waitTime = parseInt("53");    
if (waitTime > 20) {    
    console.log('a lot of time'); // use console.log rather than return
} else if (waitTime < 20) {    
    console.log('we can wait'); // use console.log rather than return
}

console.log(waitTime);

http://jsbin.com/kebagodura/edit?js,console,output

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the super quick response ! You were right i got mixed up with Python and used return instead of console log.
Your output: a lot of time 53 Correct output: a lot of time
@MarioGomez you were getting the error because without being inside a function, using return will cause a runtime error. If you are just within the global scope there is nothing to return, so an error is thrown.
2

Alternatively (to Jonathan's answer), put the code in a function and then the returns would make sense. An improved version would see you passing in the wait time as an argument to the function.

function doIt(number) {

  // Don't forget the radix on parseInt
  var waitTime  = parseInt(number, 10);
  if (waitTime > 20) {
    return 'a lot of time'
  } else if (waitTime < 20) {
    return 'we can wait'
  }
}

var result = doIt('53'); // a lot of time
var result = doIt('12'); // we can wait

DEMO

1 Comment

Thanks !! Makes sense as a function
1

You could also do it this way- instead of "return" you can do this:

var waitTime  = parseInt("53");
if (waitTime > 20) {
  waitTime = 'a lot of time';
} else if (waitTime < 20) {
  waitTime = 'we can wait';
}

console.log(waitTime);

2 Comments

For clarity you might think about using a different variable name for the messages.
Thanks! I ended out doing something similar
1

You simply can do that this way, reassigning the variable values in the if/else clause:

var waitTime = parseInt("53");

if (waitTime > 20) {
   waitTime = 'a lot of time';
} else if (waitTime <= 20) {
   waitTime = 'we can wait';
}

console.log(waitTime);

3 Comments

Thanks! Since my class is very basic I ended using this version ! I modified it a little bit because I realized when I was using 20 it returned NaN.
re: your jsbin: for some reason I never succeed to open jsbins, also not yours - do you have to be registered there to be allowed to see it?
For clarity you might think about using a different variable name for the messages.
1

You have an error in your code.

return 'a lot of time'

It is trying to return the text a lot of time from a function. but you never created a function so it will throw an error.

Closer to what you want:

function wait(waitTime) {
  if (waitTime > 20) {
    return 'a lot of time'
  } else if (waitTime < 20) {
    return 'we can wait'
  }
}
console.log(wait(YOUR_WAIT_TIME_HERE));

EDIT You don't need the parseInt function at all. (ie: '25' > 20) implicitly casts '25' to a Number.

2 Comments

If you're passing in a value for waitTime why are you declaring it again in the function. And shouldn't you be passing in a string, not waitTime?
was a typo @Andy , thnks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.