• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Nested if

Aly

New Coder
Hello!
Please tell me someone what s wrong is with this code:

JavaScript:
var n = "Alice";
var l = "Field";
var name = prompt("Enter your name");
var lastName = prompt("Enter your last name");
if (name === n) {
    if (lastName === l) {
        alert("d");
    }
    else {
        alert("try again");
    }
}
else {
    alert("name not correct");
}
 
Last edited by a moderator:

Antero360

King Coder
Staff Team
Security Analyst
Hello!
Please tell me someone what s wrong is with this code:

JavaScript:
var n = "Alice";
var l = "Field";
var name = prompt("Enter your name");
var lastName = prompt("Enter your last name");
if (name === n) {
    if (lastName === l) {
        alert("d");
    }
    else {
        alert("try again");
    }
}
else {
    alert("name not correct");
}
Hi there,
I took the liberty of giving your code a bit of formatting, for readability purposes. Is there any error messages you are getting?
 

Aly

New Coder
Hi there,
I took the liberty of giving your code a bit of formatting, for readability purposes. Is there any error messages you are getting?
Now I see everything is alright. Maybe I did understand something wrong. I thought the message "name not correct" will appear immediately after I wrote incorrectly the name, but its not. My mistake
 

Antero360

King Coder
Staff Team
Security Analyst
Now I see everything is alright. Maybe I did understand something wrong. I thought the message "name not correct" will appear immediately after I wrote incorrectly the name, but its not. My mistake
It happens :) For future reference: wrap your code like so. Save yourself a bit of a headache lol. Also, when working on your code, always remember to indent when needed. Trust me, learned this lesson the hard way myself back when I was first learning js.
1674071175411.png
 

Aly

New Coder
It happens :) For future reference: wrap your code like so. Save yourself a bit of a headache lol. Also, when working on your code, always remember to indent when needed. Trust me, learned this lesson the hard way myself back when I was first learning js.
View attachment 1964
Thank you so much! I have started to learn js from one book, and there for now is only basic staff, but yes, thank you, ill wrap my code like that in the future learning.
 

cbreemer

King Coder
I know most people will disagree, but to new programmers I recommend using Allman bracketing style so you can easily see if brackets match or not.
C:
if (name === n)
{
    if (lastName === l)
    {
        alert("d");
    }
    else
    {
        alert("try again");
    }
}
else
{
    alert("name not correct");
}
With K&R style I find that sometimes hard to verify especially with complex nesting.
 

Antero360

King Coder
Staff Team
Security Analyst
I know most people will disagree, but to new programmers I recommend using Allman bracketing style so you can easily see if brackets match or not.
C:
if (name === n)
{
    if (lastName === l)
    {
        alert("d");
    }
    else
    {
        alert("try again");
    }
}
else
{
    alert("name not correct");
}
With K&R style I find that sometimes hard to verify especially with complex nesting.
@Aly I second @cbreemer 's recommendation. Using this code style makes it much easier to spot missmatchings
 
Top