Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
Double question javascript functions with arrays

This is a double question because I can just post once every 90 minutes. First I have to write a function that replaces a character of a string.

//======================  EXAMPLE  ========================
var str = "I,Really,Like,Pizza";
characterRemover(str, ",");
"I Really Like Pizza"; // <======  EXPECTED OUTPUT
//=========================================================

And puts a space in place of the chosen character. I tried this but is not working.

function chracterRemover(str, cha){
    var replaced = str.split('cha').join(' ');
    return replaced;
}

It returns just the same string.

And the second thing is that I have to write a function that returns true if the data type introduced is an arrat and false for the rest.

//======================  EXAMPLE  ========================
var one = { name: "antonello" };
false; // <======  EXPECTED OUTPUT
var two = ["name", "antonello"];
true; // <======  EXPECTED OUTPUT
var three = [[], [], {}, "antonello", 3, function() {}];
true; // <======  EXPECTED OUTPUT
//=========================================================

I've tried this.

function isArrayFun(array){
    if {
        typeof array = 'array';
        return "Array";
    } else {
        return "Not an array"
    }
}

But as well, it doesnt work.

I get this error:

Uncaught SyntaxError: Unexpected token '{'

I don't know why. Thanks in advance for the help.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Problem 1:

You quoted cha so it's a literal string, not the variable. Use

function characterRemover(str, cha) {
  var replaced = str.split(cha).join(' ');
  return replaced;
}

var str = "I,Really,Like,Pizza";
console.log(characterRemover(str, ","));

Problem 2:

typeof returns object for arrays. The way to tell if something is an array is by calling Array.isArray().

You also have syntax errors in your if statement. The condition has to be inside (), not after {.

function isArrayFun(array) {
  if (Array.isArray(array)) {
    return "Array";
  } else {
    return "Not an array"
  }
}

var one = { name: "antonello" };
console.log(isArrayFun(one));
var two = ["name", "antonello"];
console.log(isArrayFun(two));
var three = [[], [], {}, "antonello", 3, function() {}];
console.log(isArrayFun(three));

about 4 years ago · Juan Pablo Isaza Relatório

0

First question.

  1. The function name is different than the function you called
  2. you should use .split(cha) and not 'cha'. split cha will actually split your string by the string you passed into that parameter. And 'cha' looks for the string 'cha'

Working example:

var str = "I,Really,Like,Pizza";

function chracterRemover(str, cha){
    var replaced = str.split(cha).join(' ');
    return replaced;
}
console.log(chracterRemover(str, ","));

You could also use a simple RegExp instead of using split and join and take the function to another level by making it globally useful via a 3rd parameter, in which you could define the replacement:

var str = "I,Really,Like,Pizza";

function chracterRemover(str, cha, chaTo){
    var reg = new RegExp(cha, "g");
    return str.replace(reg, chaTo);
}
console.log(chracterRemover(str, ",", " "));
console.log(chracterRemover(str, ",", "."));

Second question:

There is already a function like that

Array.isArray(value)

you can pass any type of data into that value, if it is an array it returns true

working example:

let type1 = [1,5,6];
let type2 = {a: 47};
let type3 = 5;
let type4 = "hello";

console.log(Array.isArray(type1))
console.log(Array.isArray(type2))
console.log(Array.isArray(type3))
console.log(Array.isArray(type4))

about 4 years ago · Juan Pablo Isaza Relatório

0

// First One
const str = "I,Really,Like,Pizza";
console.log(str.split(',').join(' '));

// Second One
function isArrayFun(array){
  return Array.isArray(array);
}

const one = { name: "antonello" };
console.log(isArrayFun(one));

const two = ["name", "antonello"];
console.log(isArrayFun(two));

const three = [[], [], {}, "antonello", 3, function() {}];
console.log(isArrayFun(three));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda