0

how to print as follows in javascript ?

if my input is "smart" then my output could be "s,m,a,r,t"

I've tried by using this logic but i got the output as s,m,a,r,t,

let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
  a = a + str[i] + ",";
}
console.log(a.trim());
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `'smart'.split('').join()`? – Hao Wu Mar 24 '21 at 07:14
  • Welcome to Stack Overflow! Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+split+word+into+characters+site:stackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Mar 24 '21 at 07:28
  • `const a = userInput[0]?.trim().split("").toString() || ""` – mplungjan Mar 24 '21 at 07:29
  • @Angeshwari Feel free to delete this massive dupe – mplungjan Mar 24 '21 at 09:51

1 Answers1

-1

You can use the split method in js

var str = "smart";
var res = str.split("").join();
console.log(res)
   //if you want it to remain an array don't do the join
Math Geek
  • 1
  • 1