This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Big O Forum

View Course » View Exercise

190 points
4fac47191061c00003001ff3_796703132
Submitted by
Matt
over 5 years ago

How do I turn my numbers into letters?

I can make it so that the output will give me one random position in the array via a pre-determined number, but I don't know how to output a letter from the array instead. Pretty much, I have the random positioning right, but I need that letter from the position and not the number.

var array = ["P", "e", "a", "n", "u", "t", "s", "R", "o", "c", "k"];
var out =
Math.floor(Math.random()11)*;
console.log(out);

The bolded part is the part I have been changing and so far have gotten to.


0 votes

permalink

The Math.floor() function outputs a number, and in this case you have it so it outputs a random number representing the location of a number in the array.

The easiest way to fix this would be to simply make the third like:
console.log(array[out]); — this outputs the letter in the position your random function found.

Hope that makes sense.

956 points
53d041317c82ca28e300005d_638259903
Submitted by
Lukas W
over 5 years ago


0 votes

permalink

In addition to what Lukas stated:

Math.random() should be multiplied by the length of the array to generate numbers from 0 to array.length-1, inclusive.

2280 points
A75a597a98430e1ca30e33e55ece8206?s=140&d=retro
Submitted by
Stuart Connall
almost 5 years ago