Skip to content
Advertisement

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.

Advertisement

Answer

// 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));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement