Skip to content

Commit

Permalink
Merge pull request mouredev#5908 from IgnacioFurio/reto45IgnacioFurioJS
Browse files Browse the repository at this point in the history
Reto mouredev#45 - javascript
  • Loading branch information
Roswell468 authored Nov 29, 2023
2 parents c630338 + a85fc82 commit d977c06
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const giveaway = (competitors) => {
let competitorsUpdated = [];
competitorsUpdated = competitors;

let action = parseInt(prompt("Selecciona que acción vas a realizar de entre la lista: \n 1. Añadir participante. \n 2. Borrar participante. \n 3. Mostrar participante. \n 4. Realizar sorteo. \n 5. Salir." ));

if(action === 1){
let newName = prompt("Inscríbete con tu nombre completo");

if(competitorsUpdated.length === 0){
competitorsUpdated.push(newName);
} else {
for(let i = 0 ; i < competitorsUpdated.length ; i++){
if(competitorsUpdated[i] === newName){
alert("Nombre ya registrado");
return giveaway(competitorsUpdated);
};
};
competitorsUpdated.push(newName);
};

alert(newName + " ha sido inscrito correctamente.");

return giveaway(competitorsUpdated);
} else if (action === 2) {

if (competitorsUpdated.length === 0){
alert("Todavía no existen participantes que eliminar.");
} else if (competitorsUpdated.length > 0) {
let deleteName = prompt("Escribe el nombre completo que quieras eliminar de entre quienes participan.");

for(let i = 0 ; i < competitorsUpdated.length ; i++){
if(competitorsUpdated[i] === deleteName){
competitorsUpdated.splice(i, 1);
alert(deleteName + " ya no esta participando en el sorteo.");
} else {
alert("Ups, participante no encontrado");
};
};
};
return giveaway(competitorsUpdated)

} else if (action === 3) {
if(competitorsUpdated.length === 0){
alert("Todavía no hay participantes");
} else {
alert("Participantes en el sorteo. \n" + competitorsUpdated);
};

return giveaway(competitorsUpdated);
} else if (action === 4) {
let random = Math.floor((Math.random() * (competitorsUpdated.length)));
let winner = competitorsUpdated[random];

competitorsUpdated.splice(random, 1);

alert(winner + " won the price.");
giveaway(competitorsUpdated);
};
};

let competitors = ["w","a","s","d"];
// let competitors = [];

giveaway(competitors);

0 comments on commit d977c06

Please sign in to comment.