Mist
Mist

Reputation: 37

Undefined boolean in an object javascript

I attempted the following task but i am stuck on defining a boolean:

Define a function getWalletFacts that receives wallet, an object.

getWalletFacts should return a sentence stating the wallet’s color and cash state.

My attempted code

const hascash = Boolean();

let wallet ={
    color:"",
    hascash:true||false,
    write: function(sentence){
        console.log(sentence);
    }
};
function getWalletFacts(wallet){
    let sentence= "my wallet is " + wallet.color+ " and  " + wallet.hascash; 
    return sentence;
}

whenever i check my answer it tells me that hascash is undefined i.e

    Expected: "My wallet is Black and has cash"
        Received: "my wallet is Black and  undefined"

from my understanding of the question hascash accepts a boolean

Given example

const wallet = {
    color: "Black",
    hasCash: true
};

getWalletFacts(wallet); // => 'My wallet is Black and has cash'

const wallet2 = {
    color: "Grey",
    hasCash: false
};

getWalletFacts(wallet2); // => 'My wallet is Grey and does not have cash'

Upvotes: 0

Views: 220

Answers (2)

MasterCoder
MasterCoder

Reputation: 1

function getWalletFacts(wallet) {
let money = " ";
if (wallet.hasCash === true) {
    money = "has cash";
}   else {
    money = "does not have cash";
}
let sentence = "My wallet is " + wallet.color + " and " + money;
return sentence;

}

Upvotes: 0

Barmar
Barmar

Reputation: 780851

It's hasCash, not hascash -- JavaScript is case-sensitive.

You also need a conditional to turn the true/false values into proper English.

function getWalletFacts(wallet) {
  let sentence = "my wallet is " + wallet.color + " and " + (wallet.hasCash ? "has cash" : "does not have cash");
  return sentence;
}

const wallet = {
    color: "Black",
    hasCash: true
};

console.log(getWalletFacts(wallet)); // => 'My wallet is Black and has cash'

const wallet2 = {
    color: "Grey",
    hasCash: false
};

console.log(getWalletFacts(wallet2)); // => 'My wallet is Grey and does not have cash'

Upvotes: 2

Related Questions