2
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.

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, is there any way to check if an alphabet or non-number(special characters) is inputted by the user, an error should be display?
You can check for isNaN() or parseInt() and then check for NaN first. If its NaN, then its invalid and return else continue with logic
something like this: if(isNaN(foo)) { }
@Perorary - This one covers all the cases - if(isNaN(parseInt(foo, 10))) { }
I'm a little confused what the '10' is doing inside of the parseInt(), can you explain, please?
|
0

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);

Comments

0

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);

Comments

0

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);

2 Comments

I decided to add a '-' to the string but I don't want it to show after the last string. Here is JS fiddle jsfiddle.net/ht3qmb46
@Peoray stop asking for everything here.
0

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(' '));

Comments

0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.