I need advice from Javascript Programmers

javascript

#1

Hi everyone,

I did a short program, tell me how can I improve it please :relieved:

//INTRODUCTION
alert("Welcome to the teacher's computer plateform !");
alert("The concept is simple, you can have a complete list of your students. Let's begin...")


//CLASS LEVEL
var classResponse = true;
var classLevel;
while(classResponse){
var classLevel = prompt("What's your class level ? CP, CE1, CE2, CM1 ou CM2 ? (french classroom))").toUpperCase();
switch(classLevel){
    case "CP" :
    case "CE1" :
    case "CE2" :
    case "CM1" :
    case "CM2" :
        if(confirm("Do you confirm what your level class is " + classLevel + " ?")){
            classResponse = false;
        }
    break;
    
    default:
        alert("Ooops... I think you what did a mistake. I'm going to ask again the question...");    
    }
}



//ARRAY AND FUNCTION PUSH CREATION
classList = [];
function addStudents(lastname,firstname){
        var student = "\n" + lastname + " " + firstname;
        classList.push(student);
}



//LOOP'S PLATEFORM
var exit = true;
while(exit){



//ADD A STUDENT
var exitAdd = true;
var askAdd = prompt("Do you want to add a student ? yes/no").toUpperCase();
if(askAdd === "YES"){
while(exitAdd){
    var studentResponse = true;
while(studentResponse){
    var lastNameStudent = prompt("Enter the lastname's student.").toUpperCase();
    var firstNameStudent = prompt("Enter the firstname's student.").toUpperCase();
    
    if(confirm("Do you confirm those informations: \n lastname : " + lastNameStudent + "   firstname : " + firstNameStudent + "  ?")){
        addStudents(lastNameStudent,firstNameStudent); 
        studentResponse = false;
    }else{
        alert("Can you write again those informations.");    
    }
}
    if(confirm("Do you have finish to add students ?")){
            exitAdd = false;
        }
}
}else if(askAdd !== "NO"){
    alert("Please, write yes or no, only !");
}



//SHOW CLASS LIST
var askShow = prompt("Do you whant to see your class list ? yes/no").toUpperCase();
if(askShow === "YES"){
    var classListJoin = classList.sort();
    alert("\t Class " + classLevel + "\n" + classListJoin);
}else if(askShow !== "NO"){
    alert("Please, write yes or no, only !");
}



//DELETE A STUDENT
var askDel = prompt("Do you want to delete a student ? yes/no").toUpperCase();
if(askDel === "YES"){
    var delResponse = true;
        while(delResponse){
            var lastNameStudent = prompt("Enter the lastname.").toUpperCase();
            var firstNameStudent = prompt("Enter the firstname.").toUpperCase();
            var delStudent = lastNameStudent + " " + firstNameStudent;
            
            for(i = 0; i < classList.length; i++){
                if(delStudent === classList[i].slice(1,(delStudent.length + 1))){
                    if(confirm("Are you sure to delete " + delStudent + "  ?")){
                        delete classList[i];
                        break;
                    }
                }
            }
            if(confirm("Do you have finish your cancellations ?")){
                delResponse = false;    
            }
        }
}else if(askDel !== "NO"){
    alert("Please, write yes or no, only !");
}



//EXIT COMPUTER PLATEFORM LOOP
var askExit = prompt("Do you want to exit the plateform ? yes/no").toUpperCase();
if(askExit === "YES"){
    exit = false;    
}else if(askExit !== "NO"){
    alert("Please, write yes or no, only !");
}

}



//GRATITUDE'S ALERT
alert("Thanks for your time, I wish you every successand " + classLevel + " !");

I await your advice. Thanks


#2

1) Make another broader prompt asking if the user would like to do one of the following options. "Add Students" / "List Students" / "Delete Students"

2) Computer Platform Loop. Make an else statement allowing the program to break the loop and start over.

3) Most of this can be done so that it'll run through the code Multiple times, AND you can chose which option you'd like to do. Right now it only runs down the list, one option at a time.

I spent a while looking this over, making edits to my response as I figured out what you were doing, and I'd like to say this is quite well made. Even though you're not a native English speaker it's nice to see something like this on the forums!


#3

Make another broader prompt, with a switch... very good idea !

I'm going to do some modifications, you'll tell me what do you think about it :smiley:

I want to thank @lolman for the interest and support with which he has followed my work, you're good ! :+1:


#4

It is distincly more clear now ! Check this out :

//INTRODUCTION
alert("Welcome to the teacher's computer plateform !");
alert("The concept is simple, you can have a complete list of your students. Let's begin...");


//CLASS LEVEL
var classResponse = true;
var classLevel;
while(classResponse){
var classLevel = prompt("What's your class level ? CP, CE1, CE2, CM1 ou CM2 ? (french classroom))").toUpperCase();
switch(classLevel){
    case "CP" :
    case "CE1" :
    case "CE2" :
    case "CM1" :
    case "CM2" :
        if(confirm("Do you confirm what your level class is " + classLevel + " ?")){
            classResponse = false;
        }
    break;
    
    default:
        alert("Ooops... I think you what did a mistake. I'm going to ask again the question...");    
    }
}



//ARRAY AND FUNCTION PUSH CREATION
classList = [];
function addStudents(lastname,firstname){
        var student = "\n" + lastname + " " + firstname;
        classList.push(student);
}



//LOOP'S PLATEFORM
var exit = true;
while(exit){
var doWhat = prompt("What do you want to do ? (1,2,3 or 4)\n1. Add student\n2. To watch class list\n3. Delete student\n4. Exit");
switch(doWhat){
//ADD A STUDENT
    case "1":
        var studentResponse = true;
        while(studentResponse){
            var lastNameStudent = prompt("Enter the lastname's student.").toUpperCase();
            var firstNameStudent = prompt("Enter the firstname's student.").toUpperCase();
    
        if(confirm("Do you confirm those informations: \n lastname : " + lastNameStudent + "   firstname : " + firstNameStudent + "  ?")){
        addStudents(lastNameStudent,firstNameStudent); 
            studentResponse = false;
        }else{
        alert("Can you write again those informations.");    
    }
}
    break;


//SHOW CLASS LIST
    case "2":
        var classListJoin = classList.sort();
        alert("\t Class " + classLevel + "\n" + classListJoin);
    break;
    
    
//DELETE A STUDENT
    case "3":
            var ok = false;
            var lastNameStudent = prompt("Enter the lastname.").toUpperCase();
            var firstNameStudent = prompt("Enter the firstname.").toUpperCase();
            var delStudent = lastNameStudent + " " + firstNameStudent;
            
            for(i = 0; i < classList.length; i++){
                if(delStudent === classList[i].slice(1,(delStudent.length + 1))){
                    if(confirm("Are you sure to delete " + delStudent + "  ?")){
                        delete classList[i];
                        var ok = true;
                    }
                }
            }
            if(ok === false){
                alert("Your student is untraceable...");    
            }
            
    break;
    
    
//EXIT COMPUTER PLATEFORM LOOP AND GRATITUDE'S ALERT
    case "4":
        alert("Thanks for your time, I wish you every successand " + classLevel + " !");
        exit = false;
    break;
        
    default:
        alert("I didn't understand...");
}
}

Do you have some exercices, sites, what you want about javascript for me ?


#5

Last correction I have besides trying to fix grammar, is to indent your open and closing brackets. Remember also that your if statements do not require a semi-colon after any of their closing brackets:

}else{
        alert("Can you write again those informations.");    
    };
     ^

So just watch your indentation and make sure that all your closing brackets are indented the same as your opening line as such:

if(Example){
    if(Example2){
       //Code
    }
}

I would give either 2 or 4 spaces every time you indent to help keep your code clean. It makes it much simpler to pair opening and closing brackets that way.


#6

Yes I agree, I did it because I have seen on an online syntax validator what it must use brackets, everywhere ! lol

But that is not correct. I won't make that mistake again :smirk: