Javascript switch statement comparisons throwing me for a loop - javascript

I'm trying to use a switch statement to convert some shortened tokens into full words, last time I did it, it worked, this time not so much. I think it's something wrong with the types, but I have no idea how to fix it. Weirdly enough, the modifier portion works correctly, but not the source part.
function keyToSource(key)
{
let fullSource, source, modifier;
if(key.includes("-"))
{
modifier = key.substring(key.indexOf("-") + 1, key.length);
source = key.substring(0, key.indexOf("-"));
}
else source = key;
switch(source)
{
case "Bo": fullSource = "Body"; break;
case "Ca": fullSource = "Capture"; break;
case "FA": fullSource = "Forearms"; break;
case "HL": fullSource = "Hindlegs"; break;
case "HS": fullSource = "Hard Shell"; break;
case "IR": fullSource = "Investigation Reward"; break;
case "PB": fullSource = "Palico Bonus"; break;
case "Pl": fullSource = "Plunderblade"; break;
case "SD": fullSource = "Shiny Drop"; break;
case "Ta": fullSource = "Tail"; break;
case "Tr": fullSource = "Track"; break;
default: fullSource = "Error"; break;
}
if(typeof modifier !== 'undefined')
{
switch(modifier)
{
case "C": fullSource += " carve"; break;
case "G": fullSource += "(Gold)"; break;
case "S": fullSource += "(Silver)"; break;
case "W": fullSource += " wound"; break;
default: fullSource = "Error" + fullSource; break;
}
}
return fullSource;
}
console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));

Your code appears to work. However, you can clean up the logic quite a lot by moving your switch statements into objects:
function keyToSource(key) {
const k = key.split("-");
const source = {
"Bo": "Body",
"Ca": "Capture",
"FA": "Forearms",
"HL": "Hindlegs",
"HS": "Hard Shell",
"IR": "Investigation Reward",
"PB": "Palico Bonus",
"Pl": "Plunderblade",
"SD": "Shiny Drop",
"Ta": "Tail",
"Tr": "Track"
};
const modifier = {
"C": " carve",
"G": "(Gold)",
"S": "(Silver)",
"W": " wound"
};
return (source[k[0]] || "") + (modifier[k[1]] || "");
}
console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));
console.log(keyToSource("PB"));
console.log(keyToSource("Ta-G"));
console.log(keyToSource("SD-W"));
console.log(keyToSource("HS-C"));
console.log(keyToSource("as- da-sdf")); // test invalid entry
Feel free to post a clarification if this isn't cutting it for you.

Related

Javascript problem with switch statement that find not case: "none"

My code has to see if there is a ball in a mapPoint and then changing the color of the pixels where the ball is by the color of the ball.
Here is my code:
function UpdateColorInMapPoints(mapPointIndexs)
{
mapPointIndexs.forEach(mapPointIndex => {
var mapPointData=ballsMap[mapPointIndex]
var ballsAtTheMapPoint=ballsMap[mapPointIndex].balls;
var temperature;
if(ballsAtTheMapPoint.length>0)
{
temperature=balls[ballsAtTheMapPoint[ballsAtTheMapPoint.length-1]].temperature;
}else
{
temperature="none1";
}
var column,line=0;
while(line<scale)
{
column=0;
while(column<scale)
{
putTemperatureColorAt(temperature,(mapPointData.horizontalPosition*scale+column)*4+(mapPointData.verticalPosition*scale+line)*4*imgWidth*scale);
column++;
}
line++;
}
});
canvasContext.putImageData(ImgData,0,0);
}
function putTemperatureColorAt(temperature,index)
{
switch(Math.round(temperature))
{
case -5:
ImgData.data[index+0]=0;
ImgData.data[index+1]=255;
ImgData.data[index+2]=255;
ImgData.data[index+3]=255;
break;
case -4:
ImgData.data[index+0]=50;
ImgData.data[index+1]=255;
ImgData.data[index+2]=200;
ImgData.data[index+3]=255;
break;
case -3:
ImgData.data[index+0]=100;
ImgData.data[index+1]=255;
ImgData.data[index+2]=150;
ImgData.data[index+3]=255;
break;
case -2:
ImgData.data[index+0]=150;
ImgData.data[index+1]=255;
ImgData.data[index+2]=100;
ImgData.data[index+3]=255;
break;
case -1:
ImgData.data[index+0]=200;
ImgData.data[index+1]=255;
ImgData.data[index+2]=50;
ImgData.data[index+3]=255;
break;
case 0:
ImgData.data[index+0]=255;
ImgData.data[index+1]=255;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case 1:
ImgData.data[index+0]=255;
ImgData.data[index+1]=200;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case 2:
ImgData.data[index+0]=255;
ImgData.data[index+1]=150;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case 3:
ImgData.data[index+0]=255;
ImgData.data[index+1]=100;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case 4:
ImgData.data[index+0]=255;
ImgData.data[index+1]=50;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case 5:
ImgData.data[index+0]=255;
ImgData.data[index+1]=0;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
case "none1":
ImgData.data[index+0]=0;
ImgData.data[index+1]=0;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
default:
console.log("Impossible to find color:"+temperature);
}
}
when i run the code, console say : "Impossible to find color: none1"
this means that temperature="none1" but if temperature="none1" the outpout of switch shouldn't be default cause there is case "none1":
case "none1":
ImgData.data[index+0]=0;
ImgData.data[index+1]=0;
ImgData.data[index+2]=0;
ImgData.data[index+3]=255;
break;
I very don't understand wath's wrong, i hope my english wasn't too bad.
Change your switch to the following:
switch (isFinite(temperature) ? Math.round(temperature) : temperature) {
// ...
}
You want to avoid rounding on a non-finite value aka an invalid Number.
Example
const process = (temperature) => {
switch (isFinite(temperature) ? Math.round(temperature) : temperature) {
case 0: return 'Zero';
case 1: return 'One';
case 'none1': return 'None';
default: throw new Error(`Unknown temperature: ${temperature}`);
}
};
console.log(process(0.2)); // Zero
console.log(process(0.8)); // One
console.log(process('none1')); // None
console.log(process(undefined)); // throw Error
Check your switch statement.
console.log(Math.round("none1"))

How to convert Unicode to Preeti (ASCII) using javascript or php?

I am new to javascript and recently joined Bachelor.In my first semester,I was given project to convert Unicode to Preeti (Nepali font) which is ASCII code.The Dictionary for conversion was listed as :
unicodeToPreetiDict = \
{
"अ": "c",
"आ": "cf",
"ा": "f",
"इ": "O",
"ई": "O{",
"र्": "{",
"उ": "p",
"ए": "P",
"े": "]",
"ै": "}",
"ो": "f]",
"ौ": "f}",
"ओ": "cf]",
"औ": "cf}",
"ं": "+",
"ँ": "F",
"ि": "l",
"ी": "L",
"ु": "'",
"ू": '"',
"क": "s",
"ख": "v",
"ग": "u",
"घ": "3",
"ङ": "ª",
"च": "r",
"छ": "5",
"ज": "h",
"झ": "´",
"ञ": "`",
"ट": "6",
"ठ": "7",
"ड": "8",
"ढ": "9",
"ण": "0f",
"त": "t",
"थ": "y",
"द": "b",
"ध": "w",
"न": "g",
"प": "k",
"फ": "km",
"ब": "a",
"भ": "e",
"म": "d",
"य": "o",
"र": "/",
"रू": "?",
"ृ": "[",
"ल": "n",
"व": "j",
"स": ";",
"श": "z",
"ष": "if",
"ज्ञ": "1",
"ह": "x",
"१": "!",
"२": "#",
"३": "#",
"४": "$",
"५": "%",
"६": "^",
"७": "&",
"८": "*",
"९": "(",
"०": ")",
"।": ".",
"्": "\\",
"ऊ": "pm",
"-": " ",
"(": "-",
")": "_"
}
Left part of character is Unicode whereas right part is Preeti font maybe.
You can check the conversion after writing code in this sample site:
http://unicode.shresthasushil.com.np/
If we give input as कलम (unicode),it must be converted to Preeti.I need source code in javascript or php to convert unicode to preeti.
One way is to hard code it with a switch
function convert(x)
{
switch(x)
{
case "अ":
return "c";
case "आ":
return "cf";
case "ा":
return "f";
case "इ":
return "O";
case "ई":
return "O{";
case "र्":
return "{";
case "उ":
return "p";
case "ए":
return "P";
case "े":
return "]";
case "ै":
return "}";
case "ो":
return "f]";
case "ौ":
return "f}";
case "ओ":
return "cf]";
case "औ":
return "cf}";
case "ं":
return "+";
case "ँ":
return "F";
case "ि":
return "l";
case "ी":
return "L";
case "ु":
return "'";
case "ू":
return '"';
case "क":
return "s";
case "ख":
return "v";
case "ग":
return "u";
case "घ":
return "3";
case "ङ":
return "ª";
case "च":
return "r";
case "छ":
return "5";
case "ज":
return "h";
case "झ":
return "´";
case "ञ":
return "`";
case "ट":
return "6";
case "ठ":
return "7";
case "ड":
return "8";
case "ढ":
return "9";
case "ण":
return "0f";
case "त":
return "t";
case "थ":
return "y";
case "द":
return "b";
case "ध":
return "w";
case "न":
return "g";
case "प":
return "k";
case "फ":
return "km";
case "ब":
return "a";
case "भ":
return "e";
case "म":
return "d";
case "य":
return "o";
case "र":
return "/";
case "रू":
return "?";
case "ृ":
return "[";
case "ल":
return "n";
case "व":
return "j";
case "स":
return ";";
case "श":
return "z";
case "ष":
return "if";
case "ज्ञ":
return "1";
case "ह":
return "x";
case "१":
return "!";
case "२":
return "#";
case "३":
return "#";
case "४":
return "$";
case "५":
return "%";
case "६":
return "^";
case "७":
return "&";
case "८":
return "*";
case "९":
return "(";
case "०":
return ")";
case "।":
return ".";
case "्":
return "\\";
case "ऊ":
return "pm";
case "-":
return " ";
case "(":
return "-";
case ")":
return "_";
}
}
console.log(convert("ऊ"));
And when you're sure it's working as intended and done testing, you can smash it into one line so you dont waste text editor space.
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
And so if you had a string of unicode characters, your code might look like this:
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
var str = "आअाअअ";
for(var i = 0; i < str.length; i++)
{
console.log(convert(str[i]));
}
Make sure that in your html file you have a <meta charset="utf-8"> tag.
If for some reason you can't get this method to work, there are some other methods I found out there regarding splitting using charCodeAt.
An example of this kind of method would be like so :
var str = "आअाअअ";
var arr = stringToArray(str);
console.log(arr);
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
function stringToArray() {
var i = 0,
arr = [],
codePoint;
while (!isNaN(codePoint = knownCharCodeAt(str, i))) {
arr.push(String.fromCodePoint(codePoint));
i++;
}
return arr;
}
if (!String.fromCodePoint) {
// ES6 Unicode Shims 0.1 , © 2012 Steven Levithan , MIT License
String.fromCodePoint = function fromCodePoint () {
var chars = [], point, offset, units, i;
for (i = 0; i < arguments.length; ++i) {
point = arguments[i];
offset = point - 0x10000;
units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point];
chars.push(String.fromCharCode.apply(null, units));
}
return chars.join("");
}
}
function knownCharCodeAt(str, idx) {
str += '';
var code,
end = str.length;
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while ((surrogatePairs.exec(str)) != null) {
var li = surrogatePairs.lastIndex;
if (li - 2 < idx) {
idx++;
}
else {
break;
}
}
if (idx >= end || idx < 0) {
return NaN;
}
code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = str.charCodeAt(idx + 1);
// Go one further, since one of the "characters"
// is part of a surrogate pair
return ((hi - 0xD800) * 0x400) +
(low - 0xDC00) + 0x10000;
}
return code;
}
I strongly recommend just using the first method I made for you though, as it is much easier to control.
You use arrays to search/replace in str_replace in PHP
$translated_msg = str_replace(["अ","आ","ा","इ","ई","र्","उ","ए","े","ै","ो","ौ","ओ","औ","ं","ँ","ि","ी","ु","ू","क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","रू","ृ","ल","व","स","श","ष","ज्ञ","ह","१","२","३","४","५","६","७","८","९","०","।","्","ऊ","-","(",")"], ["3","ª","r","5","h","´","`","6","7","8","9","0f","t","y","b","w","g","k","km","a","e","d","o","/","?","[","n","j",";","z","if","1","x","!","#","#","$","%","^","&","*","(",")",".","\\","pm"," ","-","_"], $some_msg);

Can I use this.something without defining it?

https://jsfiddle.net/b3nyzp9n/
When probabilityAPI.card() is called, it returns ' of '
It seems like this.apicardrankvar doesn't exist at all.
So when I defined apicardrankvar and apicardsuitvar, it returned "undefined of undefined".
I am trying to get it to say a card name, such as "3 of Diamonds"
var probabilityAPI = {
coin: function() {
if(Math.random() >= 0.5) {
return "Heads";
} else if(Math.random() > 0.5){
return "Tails";
}},
dice: function() {
return Math.floor(Math.random() * (Math.floor(7) - Math.ceil(1))) + Math.ceil(1);
},
spinner: function() {
return Math.floor(Math.random() * (Math.floor(9) - Math.ceil(0))) + Math.ceil(1);
},
apimarblevar: Math.floor(Math.random() * (Math.floor(6) - Math.ceil(1))) + Math.ceil(1),
marble: function(apimarblevar) {
switch(this.apimarblevar)
{
case 1:
return "A"
break;
case 2:
return "B"
break;
case 3:
return "C"
break;
case 4:
return "D"
break;
case 5:
return "E"
break;
}
},
apidreidelvar: Math.floor(Math.random() * (Math.floor(5) - Math.ceil(1))) + Math.ceil(1),
dreidel: function() {
switch(this.apidreidelvar) {
case 1:
return "Nun"
break;
case 2:
return "Gimel"
break;
case 3:
return "He"
break;
case 4:
return "Shin"
break;
}
},
apicardsuitvar: "",
apicardrankvar: "",
apicardrankrandom: Math.floor(Math.random() * (Math.floor(14) - Math.ceil(1))) + Math.ceil(1),
apicardsuit: function(){
switch(this.apidreidelvar) {
case 1:
this.apicardsuitvar = "Clubs"
break;
case 2:
this.apicardsuitvar = "Diamonds"
break;
case 3:
this.apicardsuitvar = "Hearts"
break;
case 4:
this.apicardsuitvar = "Spades"
break;
}
},
apicardrank: function() {
switch(this.apicardrankrandom) {
case 1:
this.apicardrankvar = "Ace"
break;
case 2:
this.apicardrankvar = "2"
break;
case 3:
this.apicardrankvar = "3"
break;
case 4:
this.apicardrankvar = "4"
break;
case 5:
this.apicardrankvar = "5"
break;
case 6:
this.apicardrankvar = "6"
break;
case 7:
this.apicardrankvar = "7"
break;
case 8:
this.apicardrankvar = "8"
break;
case 9:
this.apicardrankvar = "9"
break;
case 10:
this.apicardrankvar = "10"
break;
case 11:
this.apicardrankvar = "Jack"
break;
case 12:
this.apicardrankvar = "Queen"
break;
case 13:
this.apicardrankvar = "King"
break;
}
},
card: function() {
return this.apicardrankvar + " of " + this.apicardsuitvar
}
};
probabilityAPI.card();

switch case js two parameters

Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! This logic is very necessary for me, I hope for your indulgence.
var treshhold_two = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
switch (ratio) {
case 2:
treshhold = 2798,6;
break;
case 4:
treshhold = 3678,16;
break;
case 6:
treshhold = 5757,12;
break;
}
break;
case >0,01:
switch (attack, ratio) {
case 0,03,2:
treshhold = -5,75712;
break;
case 0,03,4:
treshhold = -5,75712 * 1,1;
break; // -45%
case 0,03,6:
treshhold = -5,75712 * 0,96;
break; // -52%, and etc.
...
}
break;
}
}
Try:
var treshhold_two = 0;
function Test(attack, ratio) {
if(attack == 0,01) {
switch (ratio) {
case 2:
treshhold = 2798,6;
break;
case 4:
treshhold = 3678,16;
break;
case 6:
treshhold = 5757,12;
break;
}
}
else {
switch (attack) {
case 0,03:
if(ratio==2) treshhold = -5,75712;
if(ratio==4) treshhold = -5,75712 * 1,1;
if(ratio==6) treshhold = -5,75712 * 0,96;
break;
...
}
}
}
//option:1
var treshhold = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
if(ratio==2) treshhold = 2798,6;
if(ratio==4) treshhold = 3678,16;
if(ratio==6) treshhold = 5757,12;
break;
case 0,03:
if(ratio==2) treshhold = -5,75712;
if(ratio==4) treshhold = -5,75712 * 1,1;
if(ratio==6) treshhold = -5,75712 * 0,96;
break;
// ...
}
}
}
//option:2
var treshhold = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
switch (ratio) {
case 2: treshhold = 2798,6; break;
case 4: treshhold = 3678,16; break;
case 6: treshhold = 5757,12; break;
}
case 0,03:
switch (ratio) {
case 2: treshhold = -5,75712; break;
case 4: treshhold = -5,75712 * 1,1; break;
case 6: treshhold = -5,75712 * 0,96;; break;
}
// ...
}
}
//option:3
var treshhold = 1223456;
function Test(ratio, attack) {
switch (ratio) {
case 2:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
case 4:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
case 6:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
}
// option?
var treshhold_two = 0;
function Test(attack, ratio) {
if (attack == 0.01) {
switch (ratio) {
case 2:
treshhold = 2798.6;
break;
case 4:
treshhold = 3678.16;
break;
case 6:
treshhold = 5757.12;
break;
}
}
else if (attack > 0.01) {
switch (attack, ratio) {
case 0,03,2: // what does it mean ?
treshhold = -5.75712;
break;
case 0,03,4: // what does it mean ?
treshhold = -5.75712 * 1.1;
break; // -45%
case 0,03,6: // what does it mean ?
treshhold = -5.75712 * 0.96;
break; // -52%, and etc.
...
}
}
}

How to simplify this 3 switch-case functions?

I want to simplify this long jquery/javascript code, can you help me? I still learn :)
Here's my jquery code:
$('.pagination-link').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$('.overlay-menu > ul > li > a').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$(window).mousewheel(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
I don't know if I have to use php to get the end of currentAnchor and put it as a parameters. Thank you for your help !
EDIT: I found that the default case is not necessary in my code. But I learn something new if I had my defaut case was important. So, here it's the new one:
function rondClass() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var currentClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(currentClass).animate();
}, 50);
}
$('.pagination-link').click(rondClass);
$('.overlay-menu > ul > li > a').click(rondClass);
$(window).mousewheel(rondClass);
Thank you everyone !
You could just replace active-slide- with rond.
var currentAnchor = $('body').attr('class');
var newClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(newClass).animate();
To handle the default case, you can handle this using indexOf or match:
// indexOf version
if (currentAnchor.indexOf('active-slide-') !== 0) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
// match version
if (!currentAnchor.match(/^active-slide-/)) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
Replace switch/case with programmatic approach and extract function to avoid code duplication.
var onClick = function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var slide = currentAnchor.match(/active\-slide\-(\d)/);
if (slide) {
$('#rond').removeClass().addClass('rond' + slide[1]).animate();
} else {
$('#rond').removeClass();
}
}, 50)
})
$('.overlay-menu > ul > li > a').click(onClick);
$('.pagination-link').click(onClick);

Categories