variable to take certain numbers

Asked

Viewed 260 times

0

Good afternoon, everyone.

I need a variable to always take a certain number from an order, for example:

the user types 123456789

variable to: always take the first number of the series, ie, 1

variable b: always take the second number of the series, ie, 2

and so on.

  • 1

    You can show what you’ve tried, what the doubt?

  • 3

    this is for some exercise? otherwise there is no reason to create a variable for each occurrence but an array

2 answers

3

The method split separates characters from a word when the delimiter is not informed. So:

var variavel = "123456789";
var caractere = variavel.split(''); // aqui ele separa a string

var variavel_A = caractere[0]; // primeiro caractere
var variavel_B = caractere[1]; // segundo caractere
var variavel_C = caractere[2]; // terceiro caractere
var variavel_D = caractere[3]; // quarto caractere

alert(variavel_A ); // retornará "1" 
alert(variavel_B); // retornará "2"
alert(variavel_C); // retornará "3"
alert(variavel_D); // retornará "4"

If the variable is a number, you will need to turn it into a string before using the split. Thus:

var num = 123456789; // numero
var variavel = num.toString(); // transforma em string
var caractere = variavel.split(''); // usa o split()

You can also use the charAt, thus:

var variavel = "123456789";

var variavel_A = variavel.charAt(0); // primeiro caractere
var variavel_B = variavel.charAt(1); // segundo caractere
var variavel_C = variavel.charAt(2); // terceiro caractere
var variavel_D = variavel.charAt(3); // quarto caractere

There is a more indicated mode if the variable is a String:

As suggested by @Isac and @lazyFox.

you can also use the indexing operator directly on a string, without the need to use the split or chatAt

Thus:

var variavel = "123456789";
alert(variavel[0]); // retorna 1
  • 2

    You can use the indexing operator directly over a string, without having to do .split('') first.

  • I didn’t know @Isac. I’ll improve the answer.

  • 1

    If the variable is already a string, you don’t even need charAt :) "1234"[0]. var a = 1234; a = a.toString()[0]

  • 1

    @lazyFox kkkkk... living and learning... I will edit the answer and quote this.

1

You can create the number of variables, dynamically, depending on the number of digits in the string

	var variavel = "123456789";
	//quantidade de dígitos na variável acima
	var n = variavel.length;
	var caractere = variavel.split('');
  
	 //criando as variáveis dinamicamente
	 for (var i = 0; i < n; i++)
	 {
	 var char_A = i+1;
	 var letra = (String.fromCharCode(char_A+64))
	 window["variavel" + letra] = (caractere[i]);
	 }
		
	//verificando algumas variaveis 
	console.log( "O valor da variavelA é " + variavelA);

    console.log( "O valor da variavelB é " + variavelB);
	
	console.log( "O valor da variavelE é " + variavelE);
  
  
   
	 //Para recuperar os valores de todas as variáveis
	 for (var i = 0; i <n; i++)
	 {
	 var char_A = i+1;
	 var letra = (String.fromCharCode(char_A+64))
	 console.log(window["variavel" + letra]);
	 }
   

The method split() splits a String object into an array of strings by separating the string into substrings.

The method String.fromCharCode() returns a string created by using a specific sequence of Unicode values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.