Javascript - function that retouches an object

Asked

Viewed 52 times

0

I’m trying to pass a challenge and I’m having a hard time getting through the following topics:

  1. Write a function called createCars which will receive three arguments: name, stars and brand. This function must return an object. The object that returns must have properties that are also called name, year and brand. The values assigned to these properties must be the values that are passed to the function. In addition, the object that createCars returns must have two methods:

    hasMorestrelasThan - a function that accepts a "name" object as a parameter and returns true if the name has more "stars" than the one that is passed to it as an argument, and false otherwise.

    sayMarca - a function that records the value of the "tag" property of the "name" object for the console.

What I have for this first step:

function createCar(nome, estrelas, marca) {

    myObject.nome = "nome";

    myObject.estrelas = "estrelas";

    myObject.marca= "marca";

    retun Object

}

I am a complete beginner and am trying to learn on my own. If anyone can help, I will be eternally grateful!

  • Welcome to Sopt. Take a look at tour for more information on how the site works. Please inform us about the specific difficulty, the error that occurs, where it occurs, etc. :)

1 answer

0


Hello,

Let’s start from the beginning and create your object correctly. See:

function createCar(nome, estrelas, marca) {
    myObject = {}; // cria o objeto myObject
    retun myObject;
}



Now we come to your adaptations and logics. Note:

function createCar(nome, estrelas, marca, ano) {
    myObject = {};

    myObject.nome = nome;
    myObject.estrelas = estrelas;
    myObject.marca= marca;
    myObject.ano = ano;

    myObject.hasMorestrelasThan = function (stars){
        if(this.estrelas < stars){ // pega estrelas do objeto myObject e compara com a starts (passada para a função)
            return true;
        }else{
            return false;
        }
    };

    myObject.sayMarca = function (){
        console.log(this.marca); // acessa a marca do objeto myObject
    }

    retun myObject;
}



From what I understand your doubt was more or less what you wanted. I hope I helped, hug!

  • 1

    Lucas, thank you so much! You were the hero of my day!

  • Just one thing: I don’t understand why you compare it to Stars. In this case, could I compare it to any other property? I’m really confused by this.

  • Hello, in this case you are comparing Stars (sent as parameter of myObject.hasMorestrelasThan) with myObject.stars (sent as parameter of the createCar function). If you want to compare any two elements, you can add a stars2 parameter in the function, for example. And modify the if to if(stars > stars2)... and from there it goes

  • 1

    Got it, Lucas! Thank you so much! I’ll try to solve the whole challenge! :)

Browser other questions tagged

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