Not sure why the code below is not working. It should take in a string and convert a G to a C and an A to a T and vice versa. However, it collects the input string but doesn't provide any output i.e. the alert just says "here is your reverse complement DNA"
var dnaSequence = prompt("Enter your DNA sequence here", "");
var newSequence = reverseComplement(dnaSequence);
alert("here is your reverse complemented DNA: " + newSequence);
function reverseComplement(dnaString) {
var reverseC = [];
var dnaArr = dnaString.split('');
for (var i = 0; i < dnaArr.length; i++) {
switch (dnaArr[i]) {
case 'A':
reverseC.push('T');
break;
case 'T':
reverseC.push('A');
break;
case 'C':
reverseC.push('G');
break;
case 'G':
reverseC.push('C');
break;
}
}
// Reverse and rejoin the the string
return reverseC.reverse().join('');
}
It should take in a string and convert a G to a C and an A to a T and vice versa.
Then you don't need the reverse(), because you are pushing in order.
Also, Make sure that you are entering uppercase letters into the prompt.
Else, you can force the uppercase.
This is the code with the two fixes:
function reverseComplement(dnaString) {
var reverseC = [];
var dnaArr = dnaString.toUpperCase().split('');
for (var i = 0; i < dnaArr.length; i++) {
switch (dnaArr[i]) {
case 'A':
reverseC.push('T');
break;
case 'T':
reverseC.push('A');
break;
case 'C':
reverseC.push('G');
break;
case 'G':
reverseC.push('C');
break;
}
}
// Reverse and rejoin the the string
return reverseC.join('');
}
var dnaSequence = prompt("Enter your DNA sequence here", "");
var newSequence = reverseComplement(dnaSequence);
alert("here is your reverse complemented DNA: " + newSequence);
The main lesson you need here is how to test and debug your JavaScript code.
First, get familiar with the JavaScript debugger in your browser. Instead of wondering why your code doesn't work, you can see directly what it is doing. Every modern browser has built-in JavaScript debugging tools; for example here is an introduction to the Chrome DevTools.
Second, when you are testing a function like this, don't use prompt() or alert(). Instead, provide a hard coded input string and use console.log() to display the output in the JavaScript debug console. This way you can run the same test case repeatedly. After you get one test case to work, you can add others.
There are several JavaScript testing frameworks if you want to get fancy, but to start with, simply using a hard coded input and console.log() output plus inspection in the JavaScript debugger is fine.
To make it easy to debug a function when you first write it, add a debugger; statement at the beginning. Then it will stop in the debugger and you can single-step through the code to see which parts of your function actually get executed and what all your variable values are at each step of the way.
For example (since it sounds like you were mistakenly testing with lowercase input), you might do this:
var dnaSequence = 'actg';
var newSequence = reverseComplement(dnaSequence);
console.log(newSequence);
function reverseComplement(dnaString) {
debugger;
var reverseC = [];
var dnaArr = dnaString.split('');
for (var i = 0; i < dnaArr.length; i++) {
switch (dnaArr[i]) {
case 'A':
reverseC.push('T');
break;
case 'T':
reverseC.push('A');
break;
case 'C':
reverseC.push('G');
break;
case 'G':
reverseC.push('C');
break;
}
}
// Reverse and rejoin the the string
return reverseC.reverse().join('');
}
Now, if you have the DevTools open, it will stop in the debugger at the first line of your function. You can single-step through the function to see which of the case statements it actually goes to, and you will see that it doesn't go to any of them. You can also look at the value of dnaArr[i] and see whether it matches any of the case values.
Related
//I need to add one to the total each time the error name is input. For example if I type "S" in the prompt, then it will add 1 to the total steering and if I type "W", it will add to wiper. The loop should run until i entered a null or zero value and calculate the total errors.
<html>
<head><title>Charge Calculator</title></head>
<body>
<script type="text/javascript">
//Declaring Variables
var day;
var data="";
var steering = 0;
var turbo =0;
var wiper =0;
day = prompt("Enter day: ","");
var BR="<br/>";
do
{
data = prompt("Enter Data: ","");
data = input.nextLine();
switch(data)
{
case 'S':
steering++;
break;
case 'T':
turbo++;
break;
case 'W':
wiper++;
break;
}
}
while(data == "")
document.write("day: " +day +BR); //Display destination name
document.write("Steering issue: " +steering +BR);
document.write("turbo Issue: " +turbo +BR);
document.write("wiper Issue: " +wiper +BR);
</script>
</body>
</html>
There are many things to be improved in your code. Be aware that the write() expression will potentially destroy parts of your html-based page. Find out about DOM manipulation commands instead.
The following snippet demonstrates in a very short way how you could collect your inputs. I used your prompt() method simply to show that it can be done but I would always prefer a simple input field instead.
const counts={s:0,t:0,w:0};
while (++counts[prompt("Please enter the error type code (s,t or w):").toLowerCase()]) {}
console.log("steering: "+counts.s+
"\nturbo: "+counts.t+
"\nwipers: "+counts.w);
Everything happens within the expression that calculates the result for the while condition: the input value is converted to lower case and then a property of the object counts will be incremented. This will only work (= return a "truthy" result) for already initialised properties like s, t or w. For all other cases an increment cannot be calculated, resulting in an "NaN" ("not a number") result. This will then end the while loop.
Seems like recursion could be more appropriate solution here. Though #Cartsten's one looks absolutely ok also.
function count() {
const counts = {
s: 0,
t: 0,
w: 0
};
const checkCounts = () => {
let input = prompt(
'Please enter the error type code (s,t or w):'
).toLowerCase();
if (counts[input] !== undefined) {
++counts[input];
return checkCounts();
}
};
checkCounts();
console.log(
`steering: ${counts.s} \n turbo: ${counts.t} \n wipers: ${counts.w}`
);
}
count();
Im trying to create a custom formula using Google App Script for a spreadsheet that given two different variables returns one numeric value.
Eg.
a = true and b = "something" return 50
Here is the code;
function VALOR_KM(vehiculo, cliente) {
var especial = (vehiculo == 'especial') ? true : false;
var valor = 0;
function costo(c) { valor = c };
switch (cliente) {
case 'asegurado':
if (especial) costo(80)
else costo(55);
break;
case 'particular':
if (especial) costo(90)
else costo(66);
break;
case 'AA':
costo(3);
break;
case 'audi':
costo(4);
break;
default:
costo(0);
break;
}
return valor;
};
But when i try to use it in a spreadsheet it gives me the #ERROR! "error analyzing formula" code. And I cant tell why its not working, because if i run the script like JavaScript it works.
Despite the formula being correct, it is not being called properly - which is what causes the error analizying formula error message.
As explained here:
The function separator for each Sheet is dependent on the the country chosen from File> Spreadsheet setting "Locale" - For example if you choose United States then function separator is comma but if you choose Germany then it will be a semicolon. What I have notice that each time you change the Country functions separator are automatically changed.
So essentially you just have to use the appropriate argument separator for your locale (which may be either ; or ,).
See if this works
function VALOR_KM(vehiculo, cliente) {
var valor;
switch (cliente) {
case 'asegurado':
valor = (vehiculo === 'especial') ? 80 : 55;
break;
case 'particular':
valor = (vehiculo === 'especial') ? 90 : 66;
break;
case 'AA':
valor = 3;
break;
case 'audi':
valor = 4;
break;
default:
valor = 0;
break;
}
return valor;
};
I have a js array like that:
let data = [{status:"stay",points:[1,2,3,4,5]}, {status:"move",points:[1,2,3,4,5]},{status:"stay",points:[1,2,3,4,5]}]
And I want to do some pattern match, here is my code:
switch (data){
case [{status:"move",_},{status:"stay",_},{status:"move",_}]:
console.log("successfully!")
}
And I don't care the points array, but in js the placeholder "_" not exist, actually I know other method to do this, but if we just use switch-case, can it be solved?
I am a newbie of js, anynoe knows how to do it?
If I understand what you're trying to do correctly, you might try reducing your array to a string, and then use that string in the switch statement. Something like this:
var actionString = ""
data.forEach(function(datum) {
actionString += datum.status + ' ';
});
// Remove extra space at the end
actionString.trim();
console.log(actionString); // "move stay move"
Then your switch statement would be:
switch(actionString) {
case "move stay move":
console.log('success!');
break;
case "stay move stay":
console.log('failure?');
break;
/* etc */
}
Try
switch (data.status){
case "stay":
case "move":
console.log("successfully!")
break;
}
Documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
i need to identify the data/text being submitted from <input> if it contains any of the following. youtube, vimeo,normal website, jpg/png, plain text
if a youtube link is found {
//do something
} else if a vimeo link is found {
//do something
} else if a normal website is found {
//do something
} else if a (jpg/png) is found {
//do something
} else just a text {
} //do something
as of the moment is my syntax. the youtube & vimeo regex format were taken from other posts. but im not sure how to create the proper regex for the others.
ive tried some regex generator but its so complicated to use
im also interested to know if this is the proper way of executing multiple conditional statement.
$(function() {
$(document).on('click','.submit', function () {
var data = $('#input').val();
var youtube = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
var vimeo = /^(http\:\/\/|https\:\/\/)?(www\.)?(vimeo\.com\/)([0-9]+)$/;
if (data.match(youtube)) {
alert("utube");
}
else if (data.match(vimeo)) {
alert("vimeo");
}
else if ...
});
});
There is a million different ways to do this.
The other regex you need are roughly bellow. Also it will save you a bit of a headache if you lowercase your data
var data = $("#input").val.toLowerCase();
Web url
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)/
PNG / JPG is at end of the string
/(png|jpg|jpeg)$/
Plain text i guese would be what ever is left
The most efficient way is also to use a switch statement not a big if else
like this http://www.w3schools.com/js/js_switch.asp
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
I would suggest using a switch statement when you have multiple regex conditions to check for:
var data = $('#input').val();
var youtube = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
var vimeo = /^(http\:\/\/|https\:\/\/)?(www\.)?(vimeo\.com\/)([0-9]+)$/;
var normalWebsite = /^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*#)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%#!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%#!\-\/\(\)]+))?$/;
var image = /<img\s+src\s*=\s*(["'][^"']+["']|[^>]+)>/;
switch (true) {
case youtube.test(data):
alert('youtube');
break;
case vimeo.test(data):
alert('vimeo');
break;
case normalWebsite.test(data):
alert('normal website');
break;
case image.test(data):
alert('image');
break;
default:
// Here we are assuming anything that doesn't match the above is plain text.
// You will need an additional regex if you want to make sure this doesn't contain html or code.
alert('Plain text');
break;
}
I have to filter out characters in a form. Thus I have implemented a filtering-out algorithm that works quite well and makes use of different filters (variables) according to different contexts; I have to make extended use of accented letters too.
Example:
gFilterALPHA1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'-–àâäéèêëîïôöùüûÀÂÄÉÈÊËÎIÔÖÙÛÜæÆœŒçÇ ";
Strangely enough, letters é (e acute) or è (e grave) are taken into account (seen as such), while others such as à (a grave) are not. I found the solution is using octal litterals — for instance \340 or \371 for a grave or u grave respectively.
Q1. Any clue about why é (e acute) is succesfully parsed straightforwardly while other accented letters are not?
Q2. Since writing a long string of octal literals is both cumbersome and error-prone when one wants to check or add values, does anyone have a better idea or know of a workaround?
Thanks.
OK, here is the code thg435 thinks it useful to take a look at.
function jFiltre_Champ(event, NomDuFiltre)
{
var LeChamp=event.target.value; // value est de type ARRAY
switch (NomDuFiltre)
{
case "NUM1":
LeFiltre=gFiltreNUM1;
Msg=gMessageNUM1;
break;
case "ALPHA1":
LeFiltre=gFiltreALPHA1;
Msg=gMessageALPHA1;
break;
case "DATE1":
LeFiltre=gFiltreDATE1;
Msg=gMessageDATE1;
break;
case "ALPHANUM1":
LeFiltre=gFiltreALPHANUM1;
Msg=gMessageALPHANUM1;
break;
case "ALPHANUM2":
LeFiltre=gFiltreALPHANUM2;
Msg=gMessageALPHANUM2;
break;
}
Longueur=LeFiltre.length;
for (i=0; i<LeChamp.length; i++)
{
leCar = LeChamp.charAt(i);
for (j = 0; j < Longueur; j++)
{
if (leCar==LeFiltre.charAt(j)) break;
}
if (j==Longueur)
{
alert(Msg);
/*Cf doc. pour l'algorithme de la méthode slice*/
document.getElementById(event.target.id).value=event.target.value.slice("0", i);
break;
}
}
}
Here is a English-style version: (regarding (2))
function jform_input_filter(event, filterName)
{
var current_input = event.target.value; // the value is an array
switch (filterName)
{
case "NUM1":
current_filter = gFilterNUM1;
Msg = gMessageNUM1;
break;
case "ALPHA1":
current_filter = gFilterALPHA1;
Msg = gMessageALPHA1;
break;
case "DATE1":
current_filter = gFilterDATE1;
Msg = gMessageDATE1;
break;
case "ALPHANUM1":
current_filter = gFilterALPHANUM1;
Msg = gMessageALPHANUM1;
break;
case "ALPHANUM2":
current_filter = gFilterALPHANUM2;
Msg = gMessageALPHANUM2;
break;
}
length = current_filter.length;
for (i = 0; i < current_input.length; i++)
{
leCar = current_input.charAt(i);
for (j = 0; j < length; j++)
{
if (leCar==current_filter.charAt(j)) break;
}
if (j == length)
{
alert(Msg);
/*Cf doc. pour l'algorithme de la méthode slice*/
document.getElementById(event.target.id).value=event.target.value.slice("0", i);
break;
}
}
Comments:
Personally I should not think this code useful to give an answer to the original question;
variables and comments are in French, which may render it difficult to read for some — sorry about that;
this function is associated to an 'onchange' event from within a HTML form;
'g' variables (e.g. gFiltreALPHANUM2) are broad-scope vectors defined elsewhere in the same .js file so that they are accessible to the function.
Bergi is probably right: your file is probably saved or delivered with the wrong encoding. Consider UTF-8 as a well supported encoding for the Unicode character set. To test this idea, you can temporarily adjust your script to output the a-with-acute-accent into the page, whether in a field or as a text node. Use the verbatim character in your string literal, not its octal escape code. If it comes out garbled, then the character didn't make it in its pristine form into the browser and you've got an encoding problem.
If the encoding problem is confirmed, you'll need to save your file correctly, or adjust the response character encoding, which depends on your particular web server. You can find the current encoding as delivered by your web server by using Fiddler and inspecting the Content-Type response header. If the web server already thinks your file is in the right encoding (preferably, as indicated, UTF-8), then check your text editor to make sure it saves the JavaScript file in the same exact encoding.
I'm writing this as an answer because I don't think I can comment directly on the question.