Changing variable types


#1


Build "Rock, Paper, Scissors"


The code works but I'm coming from more of a Java & C# background and noticed the variable type changes unless I'm mistaken.

In the code below, 'computerChoice' starts as a float but after the if statement, the value is now a string. Is 'computerChoice' really changing or is there something I'm not aware of in javascript? Assuming I'm right, doesn't this cause problems at times? Do devs just have to be careful about what they are doing with variables as they create larger projects?

Thanks!


var computerChoice = Math.random(); //here is a float
if (computerChoice < 0.34) {
	computerChoice = "rock"; //here is a string
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";


#2

simply answer yes. :slight_smile:

you can check the type
after your code just add

console.log(typeof computerChoice)


#3

Always appreciate the simple, direct answer. :slight_smile:

Thanks!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.