5 Replies - 430 Views - Last Post: 15 February 2013 - 10:16 AM

#1 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

assigning numbers from a string to a series of variables

Posted 11 February 2013 - 06:26 PM

So I want to take a list of numbers that is input from a prompt window lets say '1234' how do I assign these numbers to separate variables like number1=1, number2=2, number=3, number4=4. Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: assigning numbers from a string to a series of variables

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3459
  • View blog
  • Posts: 11,711
  • Joined: 12-December 12

Re: assigning numbers from a string to a series of variables

Posted 11 February 2013 - 06:32 PM

var blah="1234".split("");
console.log(blah);    //["1", "2", "3", "4"]

will place each value into an array.

You could read each character and store them in separate variables, but you would have to know how many numbers to expect.

This post has been edited by andrewsw: 11 February 2013 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: assigning numbers from a string to a series of variables

Posted 11 February 2013 - 06:42 PM

This is my pattern of thinking so far, I am completely new to Javascript and have not messed with arrays as of yet.
var number = window.prompt("Enter in a val");
		var number1; 
		var number2;
		var number3; 
		var number4;
		for(i = 0; i<=number.length; i++)
		{
			number1 = parseInt(number.charAt(0));
			number2 = pareInt(number.charAt(0));
			number3 = parseInt(number.charAt(0));
			number4 = parseInt(number.charAt(0)); 	
		}
		document.writeln(number1);
		document.writeln(number2);
		document.writeln(number3);
		document.writeln(number4);


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3459
  • View blog
  • Posts: 11,711
  • Joined: 12-December 12

Re: assigning numbers from a string to a series of variables

Posted 11 February 2013 - 06:55 PM

number1 = parseInt(number.charAt(0));

You are reading the same (first) character each time, because 0 refers to the first character. Replace this with your variable i.

 for(i = 0; i<=number.length; i++)

This should be just "<" less then otherwise you will be trying to read beyond the length of the text.

I suggest that you take some time to study arrays: your current approach isn't very flexible.

This post has been edited by andrewsw: 11 February 2013 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: assigning numbers from a string to a series of variables

Posted 14 February 2013 - 12:23 AM

View PostSnow1986, on 12 February 2013 - 02:26 AM, said:

how do I assign these numbers to separate variables like number1=1, number2=2, number=3, number4=4.

not at all. the reason is more about programming style. if those 4 variables are supposed to be a group, then use an array (like andrewsw showed). otherwise give a meaningful name to each one of it, though then the input method would make no sense (if you have different values with different meanings, then do different inputs).
Was This Post Helpful? 0
  • +
  • -

#6 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 827
  • Joined: 25-October 08

Re: assigning numbers from a string to a series of variables

Posted 15 February 2013 - 10:16 AM

Expanding on 'andrewsw's example:
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>
<input id="singleDigits" value="1234"> Enter single digits<br>
<button onclick="sDigitTest()">Display</button><p>
<div id="singleDigitsDIV"></div>
<hr>
<input id="multipleDigits" value="1 22 333 4444"> Enter digits separated by spaces<p>
<button onclick="mDigitTest()">Display</button><p>
<div id="multipleDigitsDIV"></div>

<script type="text/javascript">
function sDigitTest() {
  var sdArray = document.getElementById('singleDigits').value.split('');  
  document.getElementById('singleDigitsDIV').innerHTML = sdArray;
  if (sdArray.length == 4) {
    var n1 = sdArray[0];
    var n2 = sdArray[1];
    var n3 = sdArray[2];
    var n4 = sdArray[3];
    document.getElementById('singleDigitsDIV').innerHTML = n1+'<br>'+n2+'<br>'+n3+'<br>'+n4;
  } else { document.getElementById('singleDigitsDIV').innerHTML += '<p>4 digits not entered'; }
  document.getElementById('singleDigitsDIV').innerHTML += '<p>Should add checks for non-numeric entries, if necessary';
}

function mDigitTest() {
  var mdArray = document.getElementById('multipleDigits').value.split(' ');
  document.getElementById('multipleDigitsDIV').innerHTML = mdArray.join('<br>');
}

</script>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1