-2

I'm having an issue trying to get this code to work. Idea - get user input, assign user input to array and display array, then using pop method, take an element off the array and then display same array with one less element. Doing this for the purpose of understanding JavaScript, no practical application.

//Declare Variables//
var userInput;
var inputArray;

//Create array of objects//
var inputArray = [];

//Get Input//
var userInput = prompt("Enter numbers");

//transfer input to storage//
inputArray = userInput;

//Display new storage unit//
alert(inputStorage);

//Take one element off user input//
userInput.pop();


//Disply new input data//
alert(userInput);
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • 5
    Well `userInput` is not an array so it doesn't have a `.pop()` method. – JJJ Jun 06 '15 at 21:40
  • 1
    What's the question? – Felix Kling Jun 06 '15 at 21:42
  • Why are you even trying to use an array here? You prompt for a string and you need a string, so why try to put it in an array in between? – jfriend00 Jun 06 '15 at 21:57
  • What I was trying to do is get user input ..say 1,2,3,4,5 and place that input into an array that dispaly that array. Then take last element off the array and again display the array - last element. No reason for doing so, just learning through doing..or not doing in this case. – Craig Warren Jun 06 '15 at 22:20

1 Answers1

2

Here is working code: http://jsfiddle.net/Lcyuocu3/

var userInput = prompt("Enter numbers").split(' ');
alert(userInput);
userInput.pop();
alert(userInput);

Use str.split(' ') to get an array.

yuk
  • 945
  • 7
  • 14
  • Why even bother with and array an `.pop()`? Why not show a better way to write this code that just uses a string? You are artificially creating an array just so you can use `.pop()` when all that is ever needed here is just a string. – jfriend00 Jun 06 '15 at 21:56
  • Ok thank you, as I said, I am new to this, so I am probably going long awkward way around this, thank you for advice. – Craig Warren Jun 06 '15 at 22:23
  • Using str.split method may have created an array but the last alert does not produce any results at all. – Craig Warren Jun 06 '15 at 22:29
  • code works well, if u use commas between numbers, use str.split(' ,') , my code works with spaces between numbers – yuk Jun 06 '15 at 22:29
  • sorrry but it does not produce results on the last alert either way. – Craig Warren Jun 06 '15 at 22:32
  • This works, but there's a typo. Split on an empty string, not a space like: .split(""); which returns a char array. – Yogi Jun 06 '15 at 22:34
  • Ahha...ok made change and works fine, cheers. – Craig Warren Jun 06 '15 at 22:48