5

#PlatziCodingChallenge - Memory (parte 1)

Demian
demian
48322

18. Memory (parte 1)!

Crea un código que te muestre 4 caracteres entre los siguientes de forma aleatoria: "#$%()/&

Reglas del reto

Escribe tu comentario
+ 2
Ordenar por:
1
13624Puntos

JS:

function randomCharacter() {
  letresult = "";

  for (let i = 0; i < 4; i++) {
    let randomNumber = Math.floor(Math.random() * (41 - 34) + 34);
    let character = String.fromCharCode(randomNumber);
    result = result.concat(character);
  }

  console.log(result);
}

randomCharacter();
1

En Python 😃

importrandom
caracteres=['"','#','$','%','(',')','/','&']
nuevo=[]
for i in range(4):
    aleatorio=random.choice(caracteres)
    nuevo.append(aleatorio)
    caracteres.remove(aleatorio)
nuevo=''.join(nuevo)
print(nuevo)```
1
2345Puntos

Memory p1 en javascript

var caracteres = '",#,$,%,(,),/,&'var arrChar = caracteres.split(',')
varlong = arrChar.length;
var rdm = [];

for( let i = 0; i < long; i++){
    rdm[i] = arrChar[Math.round(Math.random() * (long - 1))]
}
console.log(rdm.slice(0,4).join(''));

resultado:

  1. //$%
  2. $#%/
  3. )%%/
1
19969Puntos

Memory parte 1 en Python y en JavaScript:

#Memory (parte 1) en Pythonfrom random import choice
print(choice(['"', '#', '$', '%', '(', ')', '/', '&']))
//Memory (parte 1) en JavaScript//Funcion que devuelve un elemento aleatorio de un arrayfunctionrandomChar(chars) {
    // const chars = ['"', '#', '$', '%', '(', ')', '/', '&']return chars[Math.floor(Math.random()*chars.length)]
}

//Impresion de caracter aleatorioconsole.log(randomChar(['"', '#', '$', '%', '(', ')', '/', '&']))
1
19969Puntos
7 meses

Memory parte 1 devolviendo 4 caracteres 😅:

#Memory (parte 1) en Pythonfrom random import choice

defrandomChars(chars, number):
    charsList = []
    for i in range(0, number):
        charsList.append(choice(chars))
    return charsList

caracteres = ['"', '#', '$', '%', '(', ')', '/', '&']
print(randomChars(caracteres, 4))
//Memory (parte 1) en JavaScript//Funcion que devuelve un elemento aleatorio de un arrayfunctionrandomChars(chars, number) {
    let charsList = []
    for (let i = 0; i < number; i++) {
        charsList.push(chars[Math.floor(Math.random()*chars.length)])
    }
    return charsList
}

//Impresion de caracter aleatorioconst caracteres = ['"', '#', '$', '%', '(', ')', '/', '&']
console.log(randomChars(caracteres, 4))
1
7897Puntos

Lo hice de dos formas

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Memory Parte 1</title></head><style>
  * {
    margin: 0;
  }

  .contenedor {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
  }

  .section {
    padding: 0.25rem2rem;
  }

  .inputs {
    display: inherit;
    align-items: center;
    padding: 2rem;
  }

  .btn {
    font-size: 36px;
    border: 2px solid black;
    border-radius: 6px;
    cursor: pointer;
  }

  #contra {
    padding: 020px;
    width: 95%;
  }
</style><body><divclass="contenedor"><h1style="margin-bottom: 24px;">Click para mostrar 4 caracteres entre "#$%()/& de forma aleatoria</h1><buttononclick="escribe()"class="btn">Click</button><divstyle="margin-top: 24px;"id="resultado"></div></div><script>// Forma 1// function escribe() {//   const array = ['"','#','$','%','(',')','/','&']//   const alt = []//   for (let i = 0; i < 4; i++){//     let num = Math.floor(Math.random()*8)//     alt[i] = array[num]//   }//   document.getElementById('resultado').innerHTML = `Salió ${alt.join('')} de forma aleatoria`// }// Forma 2functionescribe() {
      const array = ['34', '35', '36', '37', '38', '40', '41', '47']
      const alt = []
      for (let i = 0; i < 4; i++){
        let num = Math.floor(Math.random()*8)
        alt[i] = array[num]
      }
      let resultado = ''
      alt.map(charcode => resultado = resultado + String.fromCharCode(charcode))
      document.getElementById('resultado').innerHTML = `Salió ${resultado} de forma aleatoria`
    }
  </script></body></html>
1
26928Puntos
from random import choices

CARACTERES = ['\"', '#', '$', '%', '(', ')', '/', '&']

seleccion = choices(CARACTERES, k = 4)

simbolos = ''forchar in seleccion:
    simbolos += charprint(simbolos)
1

JavaScript

functionmemory(numbers) {
    var result           = '';
    var characters       = '"#$%()/&';
    var charactersNumbers = characters.length;
    for ( var i = 0; i < numbers; i++ ) {
       result += characters.charAt(Math.floor(Math.random() * charactersNumbers));
    }
    return result;
 }
 
 console.log(memory(4));```
1
6327Puntos

Memory en Java ☕️ ☕️ 😃

public class Memory {
    publicstaticvoid main(String[] args) {
        char[] allowedCharacters = "\"#$%()/&".toCharArray();
        StringBuffer cadena = new StringBuffer();
        SecureRandom random = new SecureRandom();

        for (int i = 0; i < 4; i++) {
            cadena.append(allowedCharacters[ random.nextInt(allowedCharacters.length) ]);
        }
        System.out.println(cadena);
    }
}

Resultado de la ejecución:

  1. /("#
  2. )"&#
  3. ()/(
  4. /)#"
1

En Python:

""" Programa que muestra de forma aleatoria 4 caracteres entre los siguientes: "#$%()/&
Program that randomly displays 4charactersamongthe following: "#$%()/& """

import randomif __name__ == "__main__":
    print('Memory part 1!!!')
    chars = ['"','#','$','%','(',')','/','&']
    word_chars = ''for i in range(1,5):
        word_chars += random.choice(chars)
    print('Your 4 random characters are: {}' .format(word_chars))
    
1
7087Puntos

JavaScript:

function makeid(length) {
   varresult           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!|ªº·$~%€&¬/()=?¿¡^+¨Ç_-.:;';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   returnresult;
}

console.log(makeid(4));
1
18993Puntos
const memory1 = () => {
    const characters = ['"','#','$','%','(',')','/','&'];
    let result = [];for(let i = 0; i < 4; i++) {
        result += characters[Math.floor(Math.random() * characters.length)]; 
    }
    console.log(result) 
}
memory1()
1
9082Puntos

Java

publicclass Main {
    publicstaticvoid main(String[] args) {
        String[] caracteres = {"\"", "#", "$", "%", "(", ")", "/", "&"};
        for (int i = 0; i < 4; i++) {
            intrandom = (int) (Math.random() * 7);
            System.out.println(caracteres[random]);
        }
    }
}
1
14668Puntos

JS

const memory_heap_1 = () =>{
    let characters = '#$%()/&'
    let random = Math.floor(Math.random() * (0,7))
    // console.log(characters[random])
    return characters[random]
}

export default memory_heap_1

Python

import random

defmemory_heap_1():
    characters = '#$%()/&'
    rd_number = random.randrange(0,7)
    return characters[rd_number]
1
14668Puntos

JS

const memory_heap_1 = () =>{
    const characters = '#$%()/&'
    let memory = []
    for(let i=0;i<4;i++){
        let random = Math.floor(Math.random() * (0,7))
        memory.push(characters[random])
    }
    
    return memory
}


Python

import random

defmemory_heap_1():
    characters = '#$%()/&'
    memory = []
    for i in range(4):
        rd_number = random.randrange(0,7)
        memory.append(characters[rd_number])
    
    return memory
0
7330Puntos
importrandom

caracteres = ['#','$','%','(',')','/','&']
aleatorio = []

for i in range(4):
    Seleccion = random.choice(caracteres)
    aleatorio.append(Seleccion)


respuesta= ' '.join(aleatorio)
print(respuesta)
0

Python 3:

import random


defmain():
    caracteres = ['#','$','%','(',')','/','&']
    for i in range(4):
        print(f'Caracter {i + 1} : {random.choice(caracteres)}')


if __name__ == '__main__':
    main()
0
12412Puntos

En Python:

import random

character = '"#$%()/&'memory = []

for i in range(4):
  simbolo = character[random.randint(0,len(character))-1]
  memory.append(simbolo)
  resultado = ''.join(memory)
  
print(resultado)

0
17607Puntos

Reto 18: Memory (parte 1)
Repositorio del reto: PlatziCodingChallengeRepo
GitHub pages: PlatziCodingChallengePages

let lista = ['"','#','$','%', '(', ')', '/', '&'];
			functionmemory(lista, i) {letindex = Math.floor(Math.random() * (lista.length - 1));
				if(i === 1)
					return lista[index];
				return memory(lista, i - 1) + lista[index];
			}
0
3732Puntos
let aleatorio=" ";

let array=['#','$','%','()','/','&'];for (let i = 0; i <= 6; i++) {
    let signo=Math.floor(Math.random()*5);
    aleatorio=aleatorio+array[signo];
    i++;
}

console.log(aleatorio);