0

Hello I'm new to coding and I wanted to try how far I've learned ( not so much ) So I made this:

var Name = prompt('What is your name?') 
var Name
var Age  = prompt('What is your age?') 
var Age 
const RealAge = 2018 - Age
if (Name && RealAge) {
    console.log("Hello " + Name + " you were born in " + RealAge + ' ...')
} else if (typeof Name == 'undefined') {
    console.log("Please insert your name")
} else if (typeof Age == 'undefined' ) {
    console.log("Please insert your age")
}

I want to know how to make the console.log says "Please insert your name" if you leave the first prompt empty and "Please insert your age" if the second prompt was left empty.

I'm sorry if this is a stupid question but I just started 2 days ago and I'll appreciate your help :)

1
  • if (Name === '') and if(Age === '') should do the trick. Also, you should check that before trying to computer RealAge Commented Mar 2, 2018 at 11:53

2 Answers 2

4
if (Name && RealAge) {
    console.log("Hello " + Name + " you were born in " + RealAge + ' ...')
} else if (!Name) {
    console.log("Please insert your name")
} else if (!Age) {
        console.log("Please insert your age")
}

!Name will evaluate to true if the value is falsy (undefined, null, empty string).

Additionally, as mentioned by Frederico, it would be better to check the values before you do any calculations:

var Name = prompt('What is your name?') 
var Name
var Age  = prompt('What is your age?') 
var Age 
if (!Name) {
    console.log("Please insert your name")
} else if (!Age) {
    console.log("Please insert your age")
} else {
    const RealAge = 2018 - +Age;
    console.log("Hello " + Name + " you were born in " + RealAge + ' ...')
}

If you need to know when both were not entered:

var Name = prompt('What is your name?') 
var Name
var Age  = prompt('What is your age?') 
var Age 
if (!Name) {
    if (!Age) {
        console.log("Please insert your name and age")
    }
    else {
        console.log("Please insert your name")
    }
} else if (!Age) {
    console.log("Please insert your age")
} else {
    const RealAge = 2018 - +Age;
    console.log("Hello " + Name + " you were born in " + RealAge + ' ...')
}
Sign up to request clarification or add additional context in comments.

3 Comments

THANKS so much can you tell me what was wrong besides the undefined thing ?
Because Age was an empty string, it was evaluating to 0 in the expression 2018 - Age. Therefore, RealAge was evaluating to 2018.
Alright then, sorry if I'm asking too much but can you tell me how to set a condition for another console log message if BOTH age and name were not inserted ?
2

You do not declare a variable again and again. Also if user does not enter anything in the prompt then its value is "" and not undefined. So just check with === "".

var Name = prompt('What is your name?'); 
var Age  = prompt('What is your age?'); 
const RealAge = 2018 - +Age;
if (Name && RealAge) {
    console.log("Hello " + Name + " you were born in " + RealAge + ' ...')
} else if (Name === '') {
    console.log("Please insert your name")
} else if (Age === '' ) {
    console.log("Please insert your age")
}

3 Comments

As I said in the comment, it would be useful to check Age before calculationg RealAge.
@FedericoklezCulloca Yea logically it would make more sense but programatically absence of check wont break anything.
yeah, didn't notice the + in +Age.

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.