0

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'
James Z
  • 12,209
  • 10
  • 24
  • 44
Mist
  • 17
  • 3
  • 3
    How are you calling getWalletFacts? – James Sep 13 '22 at 18:29
  • 1
    `wallet.hascash` won't evaluate to 'has cash'; your sentence would read "My wallet is Black and true." Also, what is `const hascash = Boolean();` for? You don't reference it. – mykaf Sep 13 '22 at 18:40
  • What's the point of `hascash:true||false`? That's the same as `hashcash: true`. – Barmar Sep 13 '22 at 19:31
  • @mykaf Thanks for the feedback. So what should i do to make the sentence read "My wallet is Black and has cash"or "My wallet is Black and has no cash" – Mist Sep 13 '22 at 20:37
  • @James GetwalletFacts is called by the software when i submit my work ,the system check if i'm meeting the questions criteria. – Mist Sep 13 '22 at 20:41
  • @Barmar Ive been experimenting with different code so i'm not too sure. I've just started out learning javascript so i'm just getting familiar with it. Any suggestions on what i should do? – Mist Sep 13 '22 at 20:43
  • EIther `hascash: true` or `hascash: false` depending on what you want the value to be. – Barmar Sep 13 '22 at 20:44
  • I suspect the intent of the problem is that the wallet should have a `cashAmount` property, not just a boolean. And the cash state is whether `cashAmount` is zero or non-zero. – Barmar Sep 13 '22 at 20:45
  • @Barmar so i dont have a defined value. the system creates inputs and tests if the code will work with both true and false. – Mist Sep 13 '22 at 20:47
  • Are you sure it should be `hascash`, not `hasCash`? And I suspect `hasCash` should be a function that you call, not a property. – Barmar Sep 13 '22 at 20:48
  • Please post the full question, as there's context missing here. – Barmar Sep 13 '22 at 20:49
  • @Barmar I've added the missing given example at the bottom of the original post. Hope it helps! – Mist Sep 13 '22 at 20:56

2 Answers2

2

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'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Could you explain a bit more about the conditional to turn the true/false values into proper English – Mist Sep 13 '22 at 21:15
  • Read https://stackoverflow.com/questions/1771786/question-mark-in-javascript – Barmar Sep 13 '22 at 21:17
0
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;

}
  • The problem in the code of the user who asked the question is case sensitive of hasCash, your code is just a different style – Shraddha Goel May 15 '23 at 04:14