I am new to coding, started about a week ago by using resources such as Mark Myers' book titled "A Smarter Way to Learn Javascript" and online tutorials. I wanted to experiment with something but so far did not succeed.
In a nutshell, I want to start with three arrays. Two of which will have three values each, and the third initially an empty array. I want each value of the first array to be concatenated with each value of the second array and the result added to the third - initially empty - array. Since the first and second arrays each contain three values, there will be nine combinations altogether, resulting in nine values in the third array.
What I want to achieve is to display one of the possible three combinations for each value of the first array. I want this to happen in a random fashion, using the statements the aforementioned book already covered, ones I am already aware of, without wandering into uncharted territories.
My approach was to create three random numbers, each of which represents a value (index, to be more exact) in the third array that will house the combinations (concatenations) of the first and second arrays. Thus, I wanted the first random number to be either 0, 1 or 2 to "choose" a possible combination for the first value of the first array, the second random number to be either 3, 4 or 5 to choose a possible combination for the second value of the first array, and finally, a third random number, either 6, 7 or 8 to point to a combination for the third value of the first array.
The goal was to log the randomly selected combinations (concatenations) - one for each value of the first array - in the console. In other words, I am expecting to see three concatenations in the log. However, the console only returns one concatenation.
Can someone please shed some light what exactly is missing or wrong? I believe the problem in my code lies in the last line but so far cannot figure out what exactly is the issue. I am also unsure why Visual Studio Code (I am using the "Prettier" extension) modifies my formatting of the last line from
console.log(strNum[num1, num2, num3]);
to
console.log(strNum[(num1, num2, num3)]);
upon saving the file but maybe this has nothing to do with my issue.
My code is the following:
// Declaring variables for the operation
var str = ["a ", "b ", "c "]; // Array str to hold strings a, b and c
var num = ["1", "2", "3"]; // Array num to hold numbers 1, 2 and 3
var strNum = []; // Array strNum to hold all possible combinations of array str and array num
var k = 0; //Counter for array strNum
// Combining the contents of array str and array num and adding the combinations to array strNum
for (i = 0; i < str.length; i++) {
for (j = 0; j < num.length; j++) {
strNum[k] = str[i] + num[j];
k++;
}
}
// The first random number between with the possible values 0, 1 or 2, to determine the first pair
var bigDecimal1 = Math.random();
var improvedDecimal1 = bigDecimal1 * 3;
var RandomNum1 = Math.floor(improvedDecimal1);
// The second random number between with the possible values 3, 4 or 5, to determine the second pair
var bigDecimal2 = Math.random();
var improvedDecimal2 = bigDecimal2 * 3 + 3;
var randomNum2 = Math.floor(improvedDecimal2);
// The third - and last - random number between with the possible values 6, 7 or 8, to determine the third pair
var bigDecimal3 = Math.random();
var improvedDecimal3 = bigDecimal3 * 3 + 6;
var randomNum3 = Math.floor(improvedDecimal3);
console.log(strNum[(num1, num2, num3)]);
Aucun commentaire:
Enregistrer un commentaire