Doubt with array and functions

Asked

Viewed 86 times

0

The purpose of the code is to make each value of the arrays (x, y, z) have a "$" before, then before it was "exemplo" and turns "$exemplo".

I tried to do it this way, but the output comes out like "undefined". If it is possible to say where I was wrong and how the code (fixed) works I thank you very much.

This code is a problem that I can use a lot in the future.

function print (value)
{
        // pega o tamanho do array;
        var all = value.length;
        // repete a função de colocar "$" vezes a quantidade de elementos do array
        for (i=0; i >= all; i++)
        {
            var novo = value[i];
            var paran = novo.length;
            // coloca o "$" no termo atual do array
            for (ii = 0; ii>= paran; ii++)
            {
                var uju = novo.substr(0, -paran)+"$"+novo.substr(-paran);
                var name = "";
                //cria uma string para juntar todos os valores do array
                name = name + uju;
            }
        }
// output teste do código
    console.log(name);
}
var x = ["hehe", "hoho", "haha", "hihi", "huhu"];
var y = ["this", "is", "awesome"];
var z = ["lorem", "ipsum", "dolor", "sit", "amet", ",", "consectetur", "adipiscing", "elit"];
//print(x);
/*  var lop = "hehe";
var resultado = lop.substr(0, -4)+"$"+lop.substr(-4);
console.log(resultado);   */
print(x);
  • 2

    Have you tried the method map of arrays (Array.prototype.map)? :-)

  • I don’t understand how the map works @Luizfelipe .

  • 2

    Two things, without going into detail: 1) you are complicating much more than necessary and 2) your loops never perform because the conditions you put in are never met

  • @bfavaretto for I would repeat x times until he became equal to paran .

  • 2

    The second argument of the for is not the condition to stop, but to continue. You’re doing it backwards. And do not need another go inside to put a prefix nodes values. Neither subst and related

  • @bfavaretto understood what you meant, already fixed.

Show 1 more comment

2 answers

7


You can put the dollar in front with a single loop, which changes each of the values, transforming them into "$" + original value:

var x = ["hehe", "hoho", "haha", "hihi", "huhu"];
for (let i=0; i<x.length; i++) {
    x[i] = '$' + x[i];
}
console.log(x);

As suggested by Luiz Felipe in a comment, you can also use the method map arrays to get the same result. And the map along with modern syntax (which may not work in older browsers) is very succinct:

var x = ["hehe", "hoho", "haha", "hihi", "huhu"];
var y = x.map(el => '$' + el);
console.log(y);

  • After you told me that my for statement was wrong I fixed and progressed a lot in the program, I arrived at practically the same result as yours but in a longer way (and a lot, rs). https://pastebin.com/nkxTLspP (exceeded character limit) the other problem is that in the third array z contains "," and the function I used to remove the , output removes the z also... @bfavaretto

  • Your second loop there is done over each character of each string in the array, it makes no sense. You don’t even use his ii. And at the end of the function you convert the whole array to string, which changes everything and also doesn’t seem to have motive. Tip: try one thing at a time, separately.

-1

Another alternative is to use foreach instead of . map()

var x = ["hehe", "hoho", "haha", "hihi", "huhu"];
x.forEach(  (elemento, indice) => 
          { x[indice] = "$"+elemento } 
         )

the advantage of using foreach is that you don’t need to create a new array

.map -> when you want to process an array and generate another one with the same number of elements

.find -> when you want to know if something is inside the array, or return an element only

.filter -> when you want to create an array with fewer elements than the original

.foreach -> when you want to process each element of the array, modifying or not.

.reduce -> when you want to calculate something using the array elements and returning a single value or an object only

Browser other questions tagged

You are not signed in. Login or sign up in order to post.