Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda