1

I need a function that takes a characters array and returns the same array but reversed and without the "*" character.

I have tried a lot of codes without success. This was my last attempt (sorry if doesn't have sense, I am learning JS)


function laClaveSecreta(carac){
    let new_str=[];
    for(i=0;i<carac.length;i++){
        if(carac[i]==="*"){
            new_str=new_str.push(carac.replace(/[*\s]/g, ''));
            
        }
        return new_str.reverse();
    }
    
 
}

//Running the function with this parameter

laClaveSecreta( [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"] )

//Result I am looking for

"El día es lunes"  
1
  • it would be easier to convert the array to a string first, then you can use replace on the entire string. carac.reverse().join("").replace(/[*]/g, "") Commented Mar 3, 2021 at 4:17

6 Answers 6

1

It would be easier to convert the array to a string first, then you can use replace on the entire string -

const input =
  [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"]

console.log(input.reverse().join("").replace(/[*]/g, ""))

El día es lunes

This isn't going to teach you much though. I assume this is a homework assignment or something. Using built-in functions like reverse, join, and replace are higher-level. You can solve the problem using a very basic for loop -

function laClaveSecreta(carac)
{ let r = ""
  for (const c of carac)
    if (c == "*")
      continue
    else
      r = c + r
  return r
}

const input =
  [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"]

console.log(laClaveSecreta(input))

El día es lunes
Sign up to request clarification or add additional context in comments.

Comments

1

Try using let filteredArray = carac.filter( char => char != '*') then return filteredArray.reverse().join("")

Comments

0

I have filtered the array, reversed it and then I joined it without regex. The code looks cleaner and explains the steps.

const laClaveSecreta = (arr) => arr.filter(arrItem => arrItem !== '*').reverse().join('');

laClaveSecreta( [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"] )

2 Comments

why bother checking arr.length? filter, reverse and join also work on empty arrays. why bother with variable assignment newStr when you can just write return arr.filter(...)... instead?
Just an old habit to make code readable and fail-proof but good suggestions
0

you can do that this way...

const laClaveSecreta  = carac =>
  {
  let new_str = '';
  for(i=carac.length;i--;)
    if(carac[i]!=="*") new_str +=carac[i]
  return new_str 
  }

console.log( laClaveSecreta('s*enul s*e aí*d l*E*'))

or

const laClaveSecreta  = carac => carac.reduceRight((a,c)=> (c==="*") ? a : a+c ,'')

console.log( laClaveSecreta([...'s*enul s*e aí*d l*E*']))

Comments

0

Another approach using Array.prototype.filter().

const input =
  [ "s", "*", "e", "n", "u", "l", " ", "s", "*", "e", " ", "a", "í", "*", "d", " ", "l", "*", "E", "*"]

console.log(input.reverse().filter(item => item !== "*").join(""));

Comments

-1
function laClaveSecreta(carac){
  let new_str=[];
  for(i=0;i<carac.length;i++){
    if(carac[i] !=="*"){
        new_str.push(carac[i]);
    }
}
 return new_str.reverse();
}

1 Comment

While this piece of code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users refer to and eventually apply this knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.