Home > OS >  Javascript Number To True/False
Javascript Number To True/False

Time:08-17

Can anyone help me with my code? I wanna make isItHigher to true if my number is higher than 200. What I'm getting to console is a number, not a boolean. Any ideas why?

class Example {
    constructor(title, author, amount) {
        this.title = title;
        this.author = author;
        this.books =  amount
        isItHigher(this.books);
    }
}

const exampleCall = new Example("Example Book", "Example Author", 300);
console.log('exampleCall --->', exampleCall);


function isItHigher(amount) {
    if (amount > 200) {
        return true
    }
    if (amount < 100000000) {
        return false
    }
}

CodePudding user response:

you are not assiging the 'isItHigher' function to anything, therefore you will not see it when you log the value of 'exampleCall' out. You will need to assign it like this in the constructor().

this.higher = isItHigher(this.books);

CodePudding user response:

class Example {
    constructor(title, author, amount) {
        this.title = title;
        this.author = author;
        this.books =  amount
        this.isHigher = isItHigher(this.books);
    }
}

const exampleCall = new Example("Example Book", "Example Author", 300);
console.log('exampleCall --->', exampleCall);


function isItHigher(amount) {
    if (amount > 200) {
        return true
    }
    if (amount < 100000000) {
        return false
    }
}

The function isItHigher is returning the boolean correctly but the returned value is not assigned to anything. You need to assign the returned value of the function isItHigher into a variable this.isHigher for example.

CodePudding user response:

I don't understand what you want to do exactly, but if you want your books value to be a boolean value, you should transfer the value that comes as a result of your control to books. Sorry for my bad English :(

class Example {
    constructor(title, author, amount) {
        this.title = title;
        this.author = author;
        this.books= isItHigher(amount);
    }
}

const exampleCall = new Example("Example Book", "Example Author", 300);
console.log('exampleCall --->', exampleCall);


function isItHigher(amount) {
    if (amount > 200) {
        return true
    }
    if (amount < 100000000) {
        return false
    }
}
  • Related