Javascript exercise

0

Hello, I have to do this exercise:

  

Enter a list of student names by screen. Each time a new user is entered, ask if you want to enter another one using the "Confirm" function. In case the user does not want to enter another student, show the following results by console:

     
  • Indicate the number of students who were admitted

  •   
  • Ask if "Maradona" came to the class. Do it with another function that takes an array as a parameter and returns true or false depending on whether it was found

  •   
  • Show the list of students by screen.

  •   

But I do not know how to solve the part of if Maradona came to the class.

var ingresarAlumno = confirm("Bievenido. Desea ingresar un alumno?");
var contador = 0;

if (ingresarAlumno){
    var alumno = prompt("Por favor ingrese el nombre de un alumnno");

    if (alumno != ''){
        contador = contador+1;
        console.log(alumno);
    }

    while(confirm('ingresar otro?')){
            var alumno = prompt("Por favor ingrese el nombre de un alumnno");
            if (alumno != ''){  
                contador = contador+1;
                console.log(alumno);
            }           
    }
    console.log(contador);
}
    
asked by Mariano Andrés Franco 13.05.2017 в 00:22
source

2 answers

1

You must create a Array as the statement says and save the students there.

Then with the function scroll the Array looking for if the name of Maradona is.

    var ingresarAlumno = confirm("Bievenido. Desea ingresar un alumno?");
    var contador = 0;
    var alumnos = [];
    
    if (ingresarAlumno) {
        var alumno = prompt("Por favor ingrese el nombre de un alumnno");
    
        if (alumno != '') {
            contador = contador + 1;
            console.log(alumno);
    		alumnos.push(alumno);
        }
    
        while (confirm('ingresar otro?')) {
                var alumno = prompt("Por favor ingrese el nombre de un alumnno");
                if (alumno != '') { 
                    contador = contador + 1;
                    console.log(alumno);
    				alumnos.push(alumno);
                }
        }
    	
    	if(VinoMaradona(alumnos))
    		console.log("Vino Maradona");
    	else 
    		console.log("No vino Maradona");
    	
        console.log(contador);
    }
    
    function VinoMaradona(lista)
    {
    	for (var i = 0; i < lista.length; i++) {
    		if(lista[i] == "Maradona")
    		return true;
    	}
    	
    	return false;
    }
    
answered by 13.05.2017 в 00:37
0

You are repeating the code, this you can avoid using do {...} while(...) instead of while(...) {...} do , you must also create an Array or Object to save the students that you are entering, then you will use this object to print the list of students and verify if Maradona came to class.

Example:

var contador = 0,
 listado = {};

if (confirm("Bievenido. Desea ingresar un alumno?")){
    do {
            var alumno = prompt("Por favor ingrese el nombre de un alumnno");
            if (alumno != ''){  
                contador += 1;
                listado[alumno] = contador;
                console.log(alumno);
            }           
    } while(confirm('ingresar otro?'));

    console.log(contador+' registros');
    console.log('listado '+JSON.stringify(listado));
    if('Maradona' in listado)
     console.log('Maradona vino hoy puesto '+listado['Maradona'])
}
    
answered by 21.05.2017 в 04:00