Home > OS >  Can't change JS Strings
Can't change JS Strings

Time:04-02

I'm creating a code to randomly assign three skills for my friends and I as we start a Skyrim challenge. As there's 18 skills to choose from, I wrote code generating three random numbers, and then created conditions that would assign strings containing the names of the skills to 3 variables so I could print them. When I ran it the first time there was an error saying that the string variables weren't defined, and I assumed that was because they were only defined within the parameters of my if statements. So I assigned some arbitrary letters to the strings beforehand, so that they would be changed as the numbers were generated but now it won't change the strings and only the placeholder text is printing. Here's my code

//three random numbers for skills
var num1 = Math.floor(Math.random() * 18)   1;
var num2 = Math.floor(Math.random() * 18)   1;
var num3 = Math.floor(Math.random() * 18)   1;
let skill1 = "as ";
let skill2 = " sd";
let skill3 = " sd";

//makes sure there's no overlap
if (num1 == num2) {
  var num2 = Math.floor(Math.random() * 18)   1;
}
if (num1 == num3) {
  var num3 = Math.floor(Math.random() * 18)   1;
}
if (num2 == num3) {
  var num3 = Math.floor(Math.random() * 18)   1;
}
//setting the string to the skill name based on the random number
if (num1 == 1) {
  let skill1 = "Illusion";
}
if (num1 == 2) {
  let skill1 = "Conjuration";
}
if (num1 == 3) {
  let skill1 = "Destruction";
}
if (num1 == 4) {
  let skill1 = "Restoration";
}
if (num1 == 5) {
  let skill1 = "Alteration";
}
if (num1 == 6) {
  let skill1 = "Enchanting";
}
if (num1 == 7) {
  let skill1 = "Smithing";
}
if (num1 == 8) {
  let skill1 = "Heavy Armor";
}
if (num1 == 9) {
  let skill1 = "Block";
}
if (num1 == 10) {
  let skill1 = "Two-Handed";
}
if (num1 == 11) {
  let skill1 = "One-Handed";
}
if (num1 == 12) {
  let skill1 = "Archery";
}
if (num1 == 13) {
  let skill1 = "Light Armor";
}
if (num1 == 14) {
  let skill1 = "Sneak";
}
if (num1 == 15) {
  let skill1 = "Lockpicking";
}
if (num1 == 16) {
  let skill1 = "Pickpocket";
}
if (num1 == 17) {
  let skill1 = "Speech";
}
if (num1 == 18) {
  let skill1 = "Alchemy";
}

if (num2 == 1) {
  let skill2 = "Illusion";
}
if (num2 == 2) {
  let skill2 = "Conjuration";
}
if (num2 == 3) {
  let skill2 = "Destruction";
}
if (num2 == 4) {
  let skill2 = "Restoration";
}
if (num2 == 5) {
  let skill2 = "Alteration";
}
if (num2 == 6) {
  let skill2 = "Enchanting";
}
if (num2 == 7) {
  let skill2 = "Smithing";
}
if (num2 == 8) {
  let skill2 = "Heavy Armor";
}
if (num2 == 9) {
  let skill2 = "Block";
}
if (num2 == 10) {
  let skill2 = "Two-Handed";
}
if (num2 == 11) {
  let skill2 = "One-Handed";
}
if (num2 == 12) {
  let skill2 = "Archery";
}
if (num2 == 13) {
  let skill2 = "Light Armor";
}
if (num2 == 14) {
  let skill2 = "Sneak";
}
if (num2 == 15) {
  let skill2 = "Lockpicking";
}
if (num2 == 16) {
  let skill2 = "Pickpocket";
}
if (num2 == 17) {
  let skill2 = "Speech";
}
if (num2 == 18) {
  let skill2 = "Alchemy";
}

if (num3 == 1) {
  let skill3 = "Illusion";
}
if (num3 == 2) {
  let skill3 = "Conjuration";
}
if (num3 == 3) {
  let skill3 = "Destruction";
}
if (num3 == 4) {
  let skill3 = "Restoration";
}
if (num3 == 5) {
  let skill3 = "Alteration";
}
if (num3 == 6) {
  let skill3 = "Enchanting";
}
if (num3 == 7) {
  let skill3 = "Smithing";
}
if (num3 == 8) {
  let skill3 = "Heavy Armor";
}
if (num3 == 9) {
  let skill3 = "Block";
}
if (num3 == 10) {
  let skill3 = "Two-Handed";
}
if (num3 == 11) {
  let skill3 = "One-Handed";
}
if (num3 == 12) {
  let skill3 = "Archery";
}
if (num3 == 13) {
  let skill3 = "Light Armor";
}
if (num3 == 14) {
  let skill3 = "Sneak";
}
if (num3 == 15) {
  let skill3 = "Lockpicking";
}
if (num3 == 16) {
  let skill3 = "Pickpocket";
}
if (num3 == 17) {
  let skill3 = "Speech";
}
if (num3 == 18) {
  let skill3 = "Alchemy";
}

let message = skill1   " "   skill2   " "   skill3;

I tried defining the string outside of the if statement, and the result was that the strings were printed as the placeholder and weren't changed by the condition being met

CodePudding user response:

Your issue is that you're redeclaring the variable within every if statement.

Instead of:

if (num1 == 4) {
  let skill1 = "Restoration";
}

Put:

if (num1 == 4) {
  skill1 = "Restoration";
}

Adding the let tells javascript you're declaring a new variable, just putting the variable name lets it know to modify your previously created variable.

CodePudding user response:

Your solution can be simplified a bit to avoid repetition:

// Use an array to store a list of all availble skills
// We're going to treat this like a deck of cards
const skills = [
    "Illusion",
    "Conjuration",
    "Destruction",
    "Restoration",
    "Alteration",
    "Enchanting",
    "Smithing",
    "Heavy Armor",
    "Block",
    "Two-Handed",
    "One-Handed",
    "Archery",
    "Light Armor",
    "Sneak",
    "Lockpicking",
    "Pickpocket",
    "Speech",
    "Alchemy",
];

// Shuffle the skills so that they have a random order in the new array
const shuffledSkills = skills.sort((a, b) => 0.5 - Math.random());

// We now have a mixed deck of cards
// Select the first three skills, i.e. pick three cards from the deck
const selectedSkills = shuffledSkills.slice(0, 3);

// Now we have an array with three random skills
console.log(selectedSkills);

// You can make a string out of them if you like
const message = selectedSkills.join(" ");

If you want to improve the randomization, this question has many good answers: How to randomize (shuffle) a JavaScript array?

  • Related