Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!