new to chrome extension: not working as expected - javascript

I'm new to Google Chrome extension development. I have very basic knowledge of html and javaScript.
I have created folder name "Extension".
in which there are 4 files as below.
1.manifest.json
{
"manifest_version": 2,
"name": "lenght Unit converter",
"description": "This extension will convert the lenght from one unit to other ",
"version": "1.0",
"browser_action": {
"default_icon": "Refresh.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
]
}
2.popup.html
<!doctype html>
<html>
<head>
<title>Length Unit Converter</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Lenght Unit Converter</h1>
<input type="text" name="inputText" id="inputValue" required onfocus="clearTextBox()">
<select id="list1" onchange="getSelectValue1()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
<br>
<input id="display" type="text" name="display" readonly>
<select id="list2" onchange="getSelectValue2()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
</br>
<br><input type="button" value="convert" onclick="setValueToTextBox()"></br>
</body>
</html>
3.popup.js
In popup.js file i have written my logic for functions which are present inside the html file.
Refresh.png
Refresh.png is 28x28 size of png format image.
Whenever i open my popup.html file with firefox it works fine. Screenshot is given below.
enter image description here
enter image description here
**Rather You Can Run my code from below here
var input1;
var input2;
var inputValue;
function clearTextBox() {
document.getElementById("display").value="";
}
function getInputValue(){
inputValue=document.getElementById("inputValue").value.toString();
}
function getSelectValue1() {
input1=document.getElementById("list1").value.toString();
}
function getSelectValue2() {
input2= document.getElementById("list2").value.toString();
document.getElementById("display").value="";
}
function setValueToTextBox()
{
document.getElementById("display").value=answer().toString();
}
function answer() {
getSelectValue1();
getSelectValue2();
getInputValue();
if (input1 == "km") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1;
break;
case "m":
return parseInt(inputValue) * 1000;
break;
case "cm":
return parseInt(inputValue) * 100000;
break;
case "mm":
return parseInt(inputValue) * 1000000;
break;
case "mc":
return parseInt(inputValue) * 1000000000;
break;
case "nm":
return parseInt(inputValue) * 1000000000000;
break;
case "ml":
return parseInt(inputValue) * 0.621371;
break;
case "yd":
return parseInt(inputValue) * 1093.61;
break;
case "ft":
return parseInt(inputValue) * 3280.84;
break;
case "ic":
return parseInt(inputValue) * 39370.1;
break;
case "ntm":
return parseInt(inputValue) * 0.539957;
break;
}
}
if (input1 == "m") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.001;
break;
case "m":
return parseInt(inputValue) * 1;
break;
case "cm":
return parseInt(inputValue) * 100;
break;
case "mm":
return parseInt(inputValue) * 1000;
break;
case "mc":
return parseInt(inputValue) * 1000000;
break;
case "nm":
return parseInt(inputValue) * 1000000000;
break;
case "ml":
return parseInt(inputValue) * 0.000621371;
break;
case "yd":
return parseInt(inputValue) * 1.09361;
break;
case "ft":
return parseInt(inputValue) * 3.28084;
break;
case "ic":
return parseInt(inputValue) * 39.3701;
break;
case "ntm":
return parseInt(inputValue) * 0.000539957;
break;
}
}
if (input1 == "cm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.00001;
break;
case "m":
return parseInt(inputValue) * 0.01;
break;
case "cm":
return parseInt(inputValue) * 1;
break;
case "mm":
return parseInt(inputValue) * 10;
break;
case "mc":
return parseInt(inputValue) * 10000;
break;
case "nm":
return parseInt(inputValue) * 10000000;
break;
case "ml":
return parseInt(inputValue) * 0.0000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0109361;
break;
case "ft":
return parseInt(inputValue) * 0.0328084;
break;
case "ic":
return parseInt(inputValue) * 0.393701;
break;
case "ntm":
return parseInt(inputValue) * 0.0000053996;
break;
}
}
if (input1 == "mm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000001;
break;
case "m":
return parseInt(inputValue) * 0.001;
break;
case "cm":
return parseInt(inputValue) * 0.1;
break;
case "mm":
return parseInt(inputValue) * 1;
break;
case "mc":
return parseInt(inputValue) * 1000;
break;
case "nm":
return parseInt(inputValue) * 1000000;
break;
case "ml":
return parseInt(inputValue) * 0.00000062137;
break;
case "yd":
return parseInt(inputValue) * 0.00109361;
break;
case "ft":
return parseInt(inputValue) * 0.00328084;
break;
case "ic":
return parseInt(inputValue) * 0.0393701;
break;
case "ntm":
return parseInt(inputValue) * 0.00000053996;
break;
}
}
if (input1 == "mc") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000000001;
break;
case "m":
return parseInt(inputValue) * 0.000001;
break;
case "cm":
return parseInt(inputValue) * 0.0001;
break;
case "mm":
return parseInt(inputValue) * 0.001;
break;
case "mc":
return parseInt(inputValue) * 1;
break;
case "nm":
return parseInt(inputValue) * 1000;
break;
case "ml":
return parseInt(inputValue) * 0.00000000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0000010936;
break;
case "ft":
return parseInt(inputValue) * 0.0000032808;
break;
case "ic":
return parseInt(inputValue) * 0.00003937;
break;
case "ntm":
return parseInt(inputValue) * 0.00000000053996;
break;
}
}
if (input1 == "nm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000000000001;
break;
case "m":
return parseInt(inputValue) * 0.000000001;
break;
case "cm":
return parseInt(inputValue) * 0.0000001;
break;
case "mm":
return parseInt(inputValue) * 0.000001;
break;
case "mc":
return parseInt(inputValue) * 0.001;
break;
case "nm":
return parseInt(inputValue) * 1;
break;
case "ml":
return parseInt(inputValue) * 0.00000000000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0000000010936;
break;
case "ft":
return parseInt(inputValue) * 0.0000000032808;
break;
case "ic":
return parseInt(inputValue) * 0.00000003937;
break;
case "ntm":
return parseInt(inputValue) * 0.00000000000053996;
break;
}
}
if (input1 == "ml") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1.60934;
break;
case "m":
return parseInt(inputValue) * 1609.34;
break;
case "cm":
return parseInt(inputValue) * 160934;
break;
case "mm":
return parseInt(inputValue) * 1609000;
break;
case "mc":
return parseInt(inputValue) * 1609000000;
break;
case "nm":
return parseInt(inputValue) * 1609000000000;
break;
case "ml":
return parseInt(inputValue) * 1;
break;
case "yd":
return parseInt(inputValue) * 1760;
break;
case "ft":
return parseInt(inputValue) * 5280;
break;
case "ic":
return parseInt(inputValue) * 63360;
break;
case "ntm":
return parseInt(inputValue) * 0.868976;
break;
}
}
if (input1 == "yd") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0009144;
break;
case "m":
return parseInt(inputValue) * 0.9144;
break;
case "cm":
return parseInt(inputValue) * 91.44;
break;
case "mm":
return parseInt(inputValue) * 914.4;
break;
case "mc":
return parseInt(inputValue) * 914400;
break;
case "nm":
return parseInt(inputValue) * 914400000;
break;
case "ml":
return parseInt(inputValue) * 0.000568182;
break;
case "yd":
return parseInt(inputValue) * 1;
break;
case "ft":
return parseInt(inputValue) * 3;
break;
case "ic":
return parseInt(inputValue) * 36;
break;
case "ntm":
return parseInt(inputValue) * 0.000493737;
break;
}
}
if (input1 == "ft") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0003048;
break;
case "m":
return parseInt(inputValue) * 0.3048;
break;
case "cm":
return parseInt(inputValue) * 30.48;
break;
case "mm":
return parseInt(inputValue) * 304.8;
break;
case "mc":
return parseInt(inputValue) * 304800;
break;
case "nm":
return parseInt(inputValue) * 304800000;
break;
case "ml":
return parseInt(inputValue) * 0.000189394;
break;
case "yd":
return parseInt(inputValue) * 0.333333;
break;
case "ft":
return parseInt(inputValue) * 1;
break;
case "ic":
return parseInt(inputValue) * 12;
break;
case "ntm":
return parseInt(inputValue) * 0.000164579;
break;
}
}
if (input1 == "ic") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0000254;
break;
case "m":
return parseInt(inputValue) * 0.0254;
break;
case "cm":
return parseInt(inputValue) * 2.54;
break;
case "mm":
return parseInt(inputValue) * 25.4;
break;
case "mc":
return parseInt(inputValue) * 25400;
break;
case "nm":
return parseInt(inputValue) * 25400000;
break;
case "ml":
return parseInt(inputValue) * 0.000015783;
break;
case "yd":
return parseInt(inputValue) * 0.0277778;
break;
case "ft":
return parseInt(inputValue) * 0.0833333;
break;
case "ic":
return parseInt(inputValue) * 1;
break;
case "ntm":
return parseInt(inputValue) * 0.000013715;
break;
}
}
if (input1 == "ic") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0000254;
break;
case "m":
return parseInt(inputValue) * 0.0254;
break;
case "cm":
return parseInt(inputValue) * 2.54;
break;
case "mm":
return parseInt(inputValue) * 25.4;
break;
case "mc":
return parseInt(inputValue) * 25400;
break;
case "nm":
return parseInt(inputValue) * 25400000;
break;
case "ml":
return parseInt(inputValue) * 0.000015783;
break;
case "yd":
return parseInt(inputValue) * 0.0277778;
break;
case "ft":
return parseInt(inputValue) * 0.0833333;
break;
case "ic":
return parseInt(inputValue) * 1;
break;
case "ntm":
return parseInt(inputValue) * 0.000013715;
break;
}
}
if (input1 == "ntm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1.852;
break;
case "m":
return parseInt(inputValue) * 1852;
break;
case "cm":
return parseInt(inputValue) * 185200;
break;
case "mm":
return parseInt(inputValue) * 1852000;
break;
case "mc":
return parseInt(inputValue) * 1852000000;
break;
case "nm":
return parseInt(inputValue) * 1852000000000;
break;
case "ml":
return parseInt(inputValue) * 1.15078;
break;
case "yd":
return parseInt(inputValue) * 2025.37;
break;
case "ft":
return parseInt(inputValue) * 6076.12;
break;
case "ic":
return parseInt(inputValue) * 72913.4;
break;
case "ntm":
return parseInt(inputValue) * 1;
break;
}
}
}
<!doctype html>
<html>
<head>
<title>Length Unit Converter</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Lenght Unit Converter</h1>
<input type="text" name="inputText" id="inputValue" required onfocus="clearTextBox()">
<select id="list1" onchange="getSelectValue1()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
<br>
<input id="display" type="text" name="display" readonly>
<select id="list2" onchange="getSelectValue2()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
</br>
<br><input type="button" value="convert" onclick="setValueToTextBox()"></br>
</body>
</html>
**
But when i load it on google chrome browser ...it looks normal as i expected. Screenshot is given below!
enter image description here
my extension looks as i expected but it's functions not work like normal html and javascript program.
Please Help me to solve this!!! thanks in advance!

Related

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);

Javascript switch statement comparisons throwing me for a loop

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.

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.
...
}
}
}

jQuery's $.append() breaks my loop, why?

I'm trying to draw some text in an array of colors to the screen. However, when using jQuery's $.append() with the <span> tag, it stops my loop and does not color anything. This is what it looks like:
var pixelsjson = '[{"red":232,"green":255,"blue":255},{"red":230,"green":255,"blue":255},{"red":229,"green":255,"blue":255},{"red":228,"green":255,"blue":255},{"red":227,"green":255,"blue":255},{"red":226,"green":255,"blue":255},{"red":224,"green":255,"blue":255},{"red":223,"green":255,"blue":255},{"red":222,"green":255,"blue":255},{"red":221,"green":254,"blue":255},{"red":220,"green":254,"blue":255},{"red":219,"green":254,"blue":255},{"red":217,"green":253,"blue":255},{"red":215,"green":253,"blue":255},{"red":213,"green":251,"blue":255},{"red":210,"green":250,"blue":255},{"red":207,"green":248,"blue":255},{"red":203,"green":246,"blue":255},{"red":200,"green":244,"blue":255},{"red":196,"green":242,"blue":255},{"red":96,"green":247,"blue":154},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54},{"red":0,"green":255,"blue":54}]]';
var pixels = JSON.parse(pixelsjson);
var imgheight = 80;
var imgwidth = 106;
$(document).ready(function () {
var drawcount = 0;
for (var i = 0; i < imgheight; i++) {
$("#image").append("<br>");
for (var j = 0; j < imgwidth; j++) {
$("#image").append("<span style='color: rgba(" + pixels[i][j].red + " , " + pixels[i][j].green + " , " + pixels[i][j].blue + ", 1);'>");
switch (drawcount % 18) {
case 0:
$("#image").append("/");
break;
case 2:
$("#image").append("-");
break;
case 3:
$("#image").append("/");
break;
case 4:
$("#image").append("-");
break;
case 5:
$("#image").append("/");
break;
case 6:
$("#image").append("-");
break;
case 7:
$("#image").append("(");
break;
case 8:
$("#image").append("8");
break;
case 9:
$("#image").append("8");
break;
case 10:
$("#image").append(")");
break;
case 11:
$("#image").append("-");
break;
case 12:
$("#image").append("\\");
break;
case 13:
$("#image").append("-");
break;
case 14:
$("#image").append("\\");
break;
case 15:
$("#image").append("-");
break;
case 16:
$("#image").append("\\");
break;
case 17:
$("#image").append(" ");
break;
}
$("#image").append("</span>");
drawcount++;
}
}
});
This is the html:
<div id="image" style="font-family:Courier New, Courier, monospace; font-size: 10px;"></div>
I updated the script to:
$(document).ready(function () {
var drawcount = 0;
for (var i = 0; i < imgheight; i++) {
$("#image").append("<br>");
for (var j = 0; j < imgwidth; j++) {
$("#image").append("<span style='position:relative; color: rgba(" + pixels[i][j].red + " , " + pixels[i][j].green + " , " + pixels[i][j].blue + ", 1);'></span>");
switch (drawcount % 18) {
case 0:
$("#image span").append("/");
break;
case 2:
$("#image span").append("-");
break;
case 3:
$("#image span").append("/");
break;
case 4:
$("#image span").append("-");
break;
case 5:
$("#image span").append("/");
break;
case 6:
$("#image span").append("-");
break;
case 7:
$("#image span").append("(");
break;
case 8:
$("#image span").append("8");
break;
case 9:
$("#image span").append("8");
break;
case 10:
$("#image span").append(")");
break;
case 11:
$("#image span").append("-");
break;
case 12:
$("#image span").append("\\");
break;
case 13:
$("#image span").append("-");
break;
case 14:
$("#image span").append("\\");
break;
case 15:
$("#image span").append("-");
break;
case 16:
$("#image span").append("\\");
break;
case 17:
$("#image span").append(" ");
break;
}
//$("#image").append("</span>");
drawcount++;
}
}
});
However, now the <br> tag isn't working (as before). However, the colors are working.

Categories