Peoray
Peoray

Reputation: 1905

How to convert number to string in JavaScript when user inputs a number

let num = prompt("enter a number");
let foo = num.split("").map(item => parseInt(item, 10));
let fig = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let arr = [
  "zero",
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine"
];
fig[0] = arr[0];
fig[1] = arr[1];
fig[2] = arr[2];
// let filteredNum = foo.filter(number => fig.includes(number));
// console.log(filteredNum);
// console.log(fig[0]);
// console.log(fig[1]);
// console.log(fig[2]);

I need help with this, I have an array of numbers (0 - 9). I provided a prompt for a user to input a number, such that that when the user inputs 1, it prints out a string 'one', if the user inputs 34, it prints out string of three four.

Upvotes: 2

Views: 272

Answers (6)

Muhammad Salman
Muhammad Salman

Reputation: 445

Code :

console.log(prompt("enter a number").split``.map(_ => ~~_).reduce((_, i) => `${_} ${"zero one two three four five six seven eight nine".split` `[i]}`, ``));

Methods :

This uses map and reduce

Explanation :

console.log(                /*begin console.log*/
    prompt(                 /*take input*/
        'enter a number'.   /*input text*/
            split``.        /*split at every single char*/
            map(            /*map over all elements of the array*/
                _ => ~~_).  /*convert to number and reduce to integer*/
                reduce(     /*begin reducing the array to one value*/
                    (_,i)=> /*parameters*/
                    `${_} ${'zero one two three four five six seven eight nine'.split` `[i]}`
                , ''))
// the last part writes value of _ and then creates a string (0-9) and splits at 
//space and then selects the value depending on i 

Upvotes: 0

Dan Nagle
Dan Nagle

Reputation: 5425

Do you need to check that the string is a number?

const num = prompt("enter a number");

const arr = [ "zero", "one", "two", "three", "four",
            "five", "six", "seven", "eight", "nine" ];

let foo = [];

if(!isNaN(num)) {
  foo = num.split('').map(n => arr[n]).join(' ');
}

console.log(foo);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386550

You could use Array.from for getting single digits (string as an iterable) and map the word if exists. If not take the value.

Later join the array with space.

var numbers = prompt("enter a number"),
    array = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

console.log(Array.from(numbers, i => array[i] || i).join(' '));

Upvotes: 0

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28445

You can try following

let num = prompt("enter a number");
let foo = num.split("").map(item => parseInt(item, 10));

let arr = ["zero","one","two","three","four","five","six","seven","eight","nine"];

console.log(foo.reduce((a,c) => a + " " + arr[c], ""));

Please see, there is no need of fig as you already have index of arr to match the numeric value of string

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

I don't think reducer is really required here. This is a simple logic and can be achieved using a simple loop too.

let num = prompt("enter a number");
let foo = num.split("").map(item => parseInt(item, 10));

let arr = ["zero","one","two","three","four","five","six","seven","eight","nine"];

var inputStr = '';
foo.forEach((num)=>{
  inputStr +=  arr[num] + ' ';
});
console.log(inputStr);

Or you can even use a one liner like this:

let num = prompt("enter a number");
let arr = ["zero","one","two","three","four","five","six","seven","eight","nine"];

let foo = num.split("").map(item => arr[parseInt(item, 10)]).join(' ');

console.log(foo);

Upvotes: 0

Faly
Faly

Reputation: 13346

You can convert your number into string and use an object as a dictionary, :

const num = prompt("enter a number");
const dict = {
  "0": "zero",
  "1": "one",
  "2": "two",
  "3": "three",
  "4": "four",
  "5": "five",
  "6": "six",
  "7": "seven",
  "8": "eight",
  "9": "nine"
}
const res = `${num}`.split('').map(e => dict[e]).join(' ');
console.log(res);

Upvotes: 0

Related Questions