FizzBuzz example in JavaScript -
working through js examples , wrote solution fizzbuzz question. prints 1..20 , str
never gets concat()
value. can please explain why doesn't work?
for(i=1; i<=20; i++){ var str = '' if(i%3===0){ str.concat('fizz') } if(i%5===0){ str.concat('buzz') } if(str===''){ console.log(i) } else { console.log(str) } }
update: since above question simple syntax error (don't want start new thread), was wondering if next way write above reply succinctly in js?
for(i=1; i<=20; i++){ var str = '' i%3===0 ? str = str.concat('fizz') : false i%5===0 ? str = str.concat('buzz') : false str==='' ? console.log(i) : console.log(str) }
because string.prototype.concat()
returns contatenated string.
you need str = str.concat("xyz");
javascript
No comments:
Post a Comment