• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Variable is not assigned problem

Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

I'm trying to to create an if/else test on the first letter of a input(bookCode) and assign a string but it doesn't seem to assign it, it alerts with nothing. Is there something I'm missing here?

 

var mockLocation = bookCode.charAt(0)
var newLocation = "";

function tpbFunction(){   
    if (mockLocation == "A"|"B") {
    newLocation = " CURRENT SCANS > A-B";
    } else if (mockLocation == "C"|"D"|"E"|"F"){
    newLocation = " CURRENT SCANS > C-F";
    } else if (mockLocation == "G"|"H"|"I"|"J"|"K"){
    newLocation = " CURRENT SCANS > G-K";
    } else if (mockLocation == "L"|"M"|"N"|"O"|"P"){
    newLocation = " CURRENT SCANS > L-P";
    } else if (mockLocation == "Q"|"R"|"S") {
    newLocation = " CURRENT SCANS > Q-S";
    } else {
    newLocation = " CURRENTSCANS > T-Z";
    }
    
} 
tpbFunction();
alert(mockLocation);
alert(newLocation);

 

TOPICS
Scripting

Views

353

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 05, 2020 Nov 05, 2020

Hi,

 

That is my bad, I didn;t spot it but you need to OR each == so instead of

 

if (mockLocation == "A"|"B")

 

you need

 

if (mockLocation == "A" || mockLocation == "B")

 

Sorry I must have read those lines a number of times and it never clicked, but that should fix it. because of the type of if you are using you could also use a switch statement something like:

 

switch ( mockLocation){
  case "A":
  case "B":
    nl = " CURRENT SCANS > A-B";
    break;
  case "C":
  case "D":
  case "E":
  c
...

Votes

Translate

Translate
Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

Let me add to that, specifically the newLocation var won't assign.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

H,

 

The variable newLocation is in two different scopes, so the external on which is effected by the lines

var newLocation = ""; AND alert ( newLocation) is actually a different variable compared to the one in tpbFunction, to marry the two you would need to alter the function, I would be tempted to just have it return a value so your code would be

 

var mockLocation = bookCode.charAt(0)

function tpbFunction(){  
    var nl = "";
    if (mockLocation == "A"|"B") {
    nl = " CURRENT SCANS > A-B";
    } else if (mockLocation == "C"|"D"|"E"|"F"){
    nl = " CURRENT SCANS > C-F";
    } else if (mockLocation == "G"|"H"|"I"|"J"|"K"){
    nl = " CURRENT SCANS > G-K";
    } else if (mockLocation == "L"|"M"|"N"|"O"|"P"){
    nl = " CURRENT SCANS > L-P";
    } else if (mockLocation == "Q"|"R"|"S") {
    nl = " CURRENT SCANS > Q-S";
    } else {
    nl = " CURRENTSCANS > T-Z";
    }
    return nl;
} 
var newLocation = tpbFunction();
alert(mockLocation);
alert(newLocation);// or if you wanted to save space << alert ( tpbFunction()) >>

 

Then the value will be passed out of the function.

 

Regards

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

I'm only just competetant in coding so if this is obvious please excuse me.

Two things.

How do I run it? Just by using the var newLocation you are running the function or do I need to run it tpbFunction(); and then assign newLocation to it. 

Lastly, when I run it now, it always pulls up the last else, it doesn't seem to be working correctly. 

else {
    nl = " CURRENTSCANS > T-Z";
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

HI,

 

1,  var newLocation = tpbFunction();  // this runs the function and puts whatever is in nl in the function into newLocation.

 

2. if it is always the if, then is it is either that the bookcode[0] is always a letter after  capital S, or it is a small letter, or not a letter at all 0-9 for example? do you know that it will always be a capital letter?

 

Regards

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

What you showed me works, thank you btw. It is always a capital letter, I think my issue is how I'm comparing the first letters but I'm not sure what yet.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

Hi,

 

That is my bad, I didn;t spot it but you need to OR each == so instead of

 

if (mockLocation == "A"|"B")

 

you need

 

if (mockLocation == "A" || mockLocation == "B")

 

Sorry I must have read those lines a number of times and it never clicked, but that should fix it. because of the type of if you are using you could also use a switch statement something like:

 

switch ( mockLocation){
  case "A":
  case "B":
    nl = " CURRENT SCANS > A-B";
    break;
  case "C":
  case "D":
  case "E":
  case "F":
    nl = " CURRENT SCANS > C-F";
    break;
  case "G":
  case "H":
  case "I":
  case "J":
  case "K":
    nl = " CURRENT SCANS > G-K";
    break;
  case "L":
  case "M":
  case "N":
  case "O":
  case "P":
    nl = " CURRENT SCANS > L-P";
    break;
  case "Q":
  case "R":
  case "S":
    nl = " CURRENT SCANS > Q-S";
    break;
  default:
    nl = " CURRENTSCANS > T-Z";
    break;
   }
    return nl;

 

 

In this case because you are always testing the same variable, the switch statement can simplify the code ( I will leave it up to you if you agree)

 

Regards

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

Thank you so much. I like the switch way better. I appriciate all your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

var mockLocation = bookCode.charAt(0)
var newLocation =  tpbFunction(mockLocation);

function tpbFunction(/*[A-Z]*/c)
{
    return " CURRENT SCANS > " +
           ( c <= 'B' && "A-B" ) ||
           ( c <= 'F' && "C-F" ) ||
           ( c <= 'K' && "G-K" ) ||
           ( c <= 'P' && "L-P" ) ||
           ( c <= 'S' && "Q-S" ) ||
           "T-Z";
}

alert(mockLocation);
alert(newLocation);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 05, 2020 Nov 05, 2020

Copy link to clipboard

Copied

LATEST

That's beautiful.

So if c is less than or equal to B then "A-B" or...

The && means if its true then A-B"?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines