Daily Challenge #310 - Boolean to String Conversion dev.to staff on January 21, 2021 Implement a function which will convert the given boolean value into its string representation. So for the boolean false, return "false" and for t... Read full post Submit Preview Dismiss Collapse Expand Brad Brad Brad Follow Full-stack TypeScript developer, loves a challenge and learning new fun things! Location Los Angeles Work Software Engineer Joined Oct 25, 2018 • Jan 21 '21 Dropdown menu Copy link Hide shortest version I can think of: javascript const boolToStr = b => '' + b; Enter fullscreen mode Exit fullscreen mode or a more safe version that wont print null or undefined, rather it will convert it to true/false depending on its "truthiness": javascript const boolToStr = b => '' + !!b; Enter fullscreen mode Exit fullscreen mode Collapse Expand Nguyen Tran Chau Giang Nguyen Tran Chau Giang Nguyen Tran Chau Giang Follow Learning and sharing everything about the IT world. Currently building https://dev.ga to teach basic programming. Location In the Cloud Joined Nov 13, 2018 • Jan 25 '21 • Edited on Jan 25 • Edited Dropdown menu Copy link Hide My solution that is submitted to Codewar const char *boolean_to_string(bool b) { return b ? "true" : "false"; } Enter fullscreen mode Exit fullscreen mode My another solution that is accepted int and char type. C using _Generic. feel free to contribute #define pt() printf("true\n") #define pf() printf("false\n") #define cast_to_boolean (x) _Generic( (x), char: x == '\0' ? pf() : pt(), int: x == 0 ? pf() : pt(), default: pf()) Enter fullscreen mode Exit fullscreen mode Collapse Expand Mihail Malo Mihail Malo Mihail Malo Follow Joined Jul 11, 2018 • Jan 22 '21 • Edited on Jan 22 • Edited Dropdown menu Copy link Hide Rust 1. return an owned String: format!("{}", v); Enter fullscreen mode Exit fullscreen mode or (on nightly) format!("{v}"); Enter fullscreen mode Exit fullscreen mode or v.to_string(); Enter fullscreen mode Exit fullscreen mode 2. return a reference to a string embedded in the binary: fn bas(v: bool) -> &'static str { match v { true => "true", false => "false", } } Enter fullscreen mode Exit fullscreen mode someone in discord golfed that to ["false", "true"][my_bool as usize] Enter fullscreen mode Exit fullscreen mode but idk idk :v Collapse Expand Nijeesh Joshy Nijeesh Joshy Nijeesh Joshy Follow Some one Who Love to Code ,Create and Experiment with Everything Location Karur Education BE in Mechatornics Engineering Work ROR Developer at Mallow technologies pvt.ltd Joined Nov 17, 2017 • Jan 24 '21 • Edited on Jan 24 • Edited Dropdown menu Copy link Hide Ruby there already exists a function to_s. If you want another one, here it is def bool_to_s(str) (!!str).to_s end Enter fullscreen mode Exit fullscreen mode It will work without the !! I just wanted to make sure it will work even if anything else is passed Collapse Expand Raphael Chaula Raphael Chaula Raphael Chaula Follow Software Engineer Location Dar es Salaam, Tanzania Education BEng in Telecommunications and Electronics Work Software Engineer - Peramiho Technologies Joined Mar 12, 2019 • Jan 21 '21 Dropdown menu Copy link Hide JavaScript const booleanToString = (value) => { return JSON.stringify(value); }; const booleanValue = true; const itsStringValue = booleanToString(booleanValue); Enter fullscreen mode Exit fullscreen mode Collapse Expand Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Follow Ultra-fullstack software developer. Python, JavaScript, C#, C. Location Earth Education I am a master of science Pronouns They/them Work Software Engineer Joined May 2, 2017 • Jan 21 '21 Dropdown menu Copy link Hide That's pretty heavyweight! value.toString() works, too. Collapse Expand Raphael Chaula Raphael Chaula Raphael Chaula Follow Software Engineer Location Dar es Salaam, Tanzania Education BEng in Telecommunications and Electronics Work Software Engineer - Peramiho Technologies Joined Mar 12, 2019 • Jan 21 '21 Dropdown menu Copy link Hide Yes, mine is really really heavy, just tried something no one could think of. Collapse Expand peter279k peter279k peter279k Follow I'm a open source project contributor from Taiwan. Location Taipei City Work Associate engineer at Institute for Information Industry Joined Apr 7, 2019 • Jan 21 '21 Dropdown menu Copy link Hide Python: def boolean_to_string(b): return str(b) Enter fullscreen mode Exit fullscreen mode Collapse Expand Juan Benitez Juan Benitez Juan Benitez Follow Software Developer with over 3 years of experience, mainly working with Python and Django. If you think you can do it, you can. Location Cartagena, Colombia Work Software Developer Joined Feb 1, 2019 • Jan 22 '21 Dropdown menu Copy link Hide boolean_to_string = lambda x: str(x) Enter fullscreen mode Exit fullscreen mode Collapse Expand Akshit Garg Akshit Garg Akshit Garg Follow Always learning 🧠. Currently reading 📖 Jean Phillippe's Serious Cryptography! Location New Delhi, India Joined Dec 19, 2018 • Jan 21 '21 Dropdown menu Copy link Hide package main import ( "fmt" "strconv" ) func main() { myBool := true fmt.Println(strconv.FormatBool(myBool)) } Enter fullscreen mode Exit fullscreen mode Some go here Collapse Expand Andrea Grillo Andrea Grillo Andrea Grillo Follow /* Life runs on code */ Location Milan, Italy Education Bachelor's Degree Work Senior Cloud Engineer @PagoPA Joined Dec 30, 2020 • Jan 22 '21 Dropdown menu Copy link Hide C# bool.TrueString.ToLower() bool.FalseString.ToLower() Code of Conduct • Report abuse
shortest version I can think of:
javascript
or a more safe version that wont print
nullorundefined, rather it will convert it to true/false depending on its "truthiness":javascript
My solution that is submitted to Codewar
My another solution that is accepted
intandchartype.C using _Generic. feel free to contribute
Rust
1. return an owned
String:or (on nightly)
or
2. return a reference to a string embedded in the binary:
someone in discord golfed that to
but idk idk :v
Ruby
there already exists a function
to_s.If you want another one, here it is
It will work without the
!!I just wanted to make sure it will work even if anything else is passedJavaScript
That's pretty heavyweight!
value.toString()works, too.Yes, mine is really really heavy, just tried something no one could think of.
Python:
Some go here
C#
bool.TrueString.ToLower()
bool.FalseString.ToLower()