pat Posted February 12, 2021 at 10:49 AM Report Share #621317 Posted February 12, 2021 at 10:49 AM Olá tenho um exercício para resolver que me ocupa a cabeça há já vários dias... Já fiz e refiz , também já li que se poderiam usar métodos regex mas não se aplica aqui, neste exercício , dado que ainda não foram abordados nesta plataforma. Vou deixar aqui o exercício, bem como a minha tentativa , na esperança que alguém me possa direccionar no caminho do que me está a falhar, sinto que estou perto mas alguma coisa falha... // exercício: Citação Create a function named extractPassword which takes an array of characters (which includes some trash characters) and returns a string with only valid characters (a - z, A - Z, 0 - 9). Here's an example: extractPassword(['a', '-', '~', '1', 'a', '/']); // should return the string 'a1a' extractPassword(['~', 'A', '7', '/', 'C']); // should return the string 'A7C' // o meu código: var password = (['a', 'º', '~', 'z', 'A', '&', 'Z', '0', '*', '9']); var newPass =[]; var arrayLength = password.length; function extractPassword(password) { for (var i = 0; i < arrayLength; i ++) { var j = password[i]; if(('a'<= j && j<= 'z') || ('A'<=j && j<='Z') || ('0'<=j && j<='9')) { newPass.push(j); } } return newPass; } extractPassword(password); console.log(' '+ newPass); // output: >>>>Code is incorrect Your function is not returning the correct value a,z,A,Z,0,9 Link to comment Share on other sites More sharing options...
tiago.f Posted February 12, 2021 at 11:07 AM Report Share #621318 Posted February 12, 2021 at 11:07 AM Olá. Se o exercício pede para definires uma função, então provavelmente o teste que o teu prof desenvolveu vai chamar essa função e não vai saber nada acerca das variáveis globais que estás a definir antes. Sugiro que removas todas as variáveis globais e fiques apenas com variáveis locais (dentro da função). Depois, deves retornar uma string e não o array newPass. Algo assim (nao testei): function extractPassword(password) { let newPass = []; for (var i = 0; i < password.length; i ++) { var j = password[i]; if(('a'<= j && j<= 'z') || ('A'<=j && j<='Z') || ('0'<=j && j<='9')) { newPass.push(j); } } return newPass.join(''); } Link to comment Share on other sites More sharing options...
pat Posted February 12, 2021 at 11:17 AM Author Report Share #621319 Posted February 12, 2021 at 11:17 AM '' deves retornar uma string e não o array newPass.'' Já percebi, obrigada!! Link to comment Share on other sites More sharing options...
jsWizard Posted February 12, 2021 at 03:58 PM Report Share #621321 Posted February 12, 2021 at 03:58 PM function extractPassword(password){ return(password.join('').replace(/[^a-zA-Z0-9]/g, '')); } Link to comment Share on other sites More sharing options...
jmcp Posted September 7, 2022 at 05:53 PM Report Share #627181 Posted September 7, 2022 at 05:53 PM Olá. Também não estou a conseguir O meu código: var password = (["a","-","~","1","a","/","~","A","7","/","C"]); var newPass =[]; var arrayLength = password.length; function extractPassword(password){ for (var i=0;i<arrayLength;i++){ var j = password[i]; if (("a"<= j && j<="z") || ("A"<=j && j<="Z") || ("0"<=j && j<="9")){ newPass.push(j); } } return newPass; } extractPassword(password); console.log(newPass.join("")); OUTPUT: >>>>Code is incorrect Your function is not returning the correct value a1aA7C _________________________ O que está a falhar? O output parece estar correto. Alguém consegue ajudar? Obrigado! Link to comment Share on other sites More sharing options...
jmcp Posted September 7, 2022 at 06:12 PM Report Share #627183 Posted September 7, 2022 at 06:12 PM On 2/12/2021 at 3:58 PM, jsWizard said: function extractPassword(password){ return(password.join('').replace(/[^a-zA-Z0-9]/g, '')); } O exercicio não aceita essa função :( Link to comment Share on other sites More sharing options...
nelsonr Posted September 8, 2022 at 07:55 AM Report Share #627186 Posted September 8, 2022 at 07:55 AM Pelo enunciado, a tua função deve retornar uma string e não um array. A função não deverá estar dependente de variáveis globais. Link to comment Share on other sites More sharing options...
jmcp Posted September 8, 2022 at 09:06 AM Report Share #627190 Posted September 8, 2022 at 09:06 AM Já consegui obrigado nelsonr! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now