Wrong value while getting last symbol in string in Javascript - javascript

I have a variable someText where number is stored. Depending on last number I need to add different text. So I convert someText into stiring, get string length in someTextLng and substract for the last symbol someTextLng.
document.write(lastChar + "<br/>"); in my example returns 7 - all ok.
Continuing with if and getting surprise - lastChar = 1. But why? Where is my mistake?
<script type="text/javascript">
var someText = 312347;
someText= someText.toString();
someTextLng = someText.length-1;
var lastChar = someText.substr(someTextLng, 1);
document.write(lastChar + "<br/>");
if (lastChar = "1") {
document.write(lastChar+" Day")
}
else if (lastChar = "2") {
document.write(lastChar+" DayZ")
}
else {
alert ("Wuza");
}
</script>

Why not use the reminder operator % for the last digit?
var last = number % 10;
and later
if (last === 1) {
// do something
}

You need to use == to match the value of lastChar. To get the lastChar you can use reminder operator:
var lastChar = someText % 10;
if (lastChar == 1) {
document.write(lastChar+" Day")
}
else if (lastChar == 2) {
document.write(lastChar+" DayZ")
}
else {
alert ("Wuza");
}

If you want to check for something that equal to something else it's === not =
Your code should be like that
if (lastChar === "1") {
document.write(lastChar+" Day")
}
else if (lastChar === "2") {
document.write(lastChar+" DayZ")
}
else {
alert ("Wuza");
}
:)

var someText = 312347;
someText= someText.toString();
someTextLng = someText.length-1;
var lastChar = someText.substr(someTextLng, 1);
console.log(lastChar + "<br/>");
if (lastChar == "1") {
console.log(lastChar+" Day")
}
else if (lastChar == "2") {
console.log(lastChar+" DayZ")
}
else {
alert ("Wuza");
}

Try this
<script type="text/javascript">
var lastChar = (312347 % 10).toString;
document.write(lastChar + "<br/>");
if (lastChar === "1") {
document.write(lastChar+" Day")
} else if (lastChar === "2") {
document.write(lastChar+" DayZ")
} else {
alert ("Wuza");
}
</script>

Related

jQuery: For loop if else and nth-child

for (i = 1; i < 24; i++){{
if(dices[i].value == 0) {
$('td div:nth-child(i)').addClass("zero-desktop")
} else if (dices[i].value == 1) {
$('td div:nth-child(i)').addClass("one-desktop")
} else if (dices[i].value == 2){
$('td div:nth-child(i)').addClass("two-desktop")
} else if (dices[i].value == 3) {
$('td div:nth-child(i)').addClass("three-desktop")
} else if (dices[i].value == 4) {
$('td div:nth-child(i)').addClass("four-desktop")
} else {
alert ("NOT WORKING")
}
}};
dices[i] is an array that displays 23 random numbers and there are 23 divs.
Each time dice[i].value equals to one of (0~4), class will be added to div and when it successfully adds a class to div, [i] will increment until it reaches the end number.
For some reason uncaught Error:syntax error, unrecognized expression: :nth-child error shows up.
The problem here is that you are literally inserting the character 'i' in your selectors instead of using the variable i. You should use string concatenation, like this:
for (i = 1; i < 24; i++) {
if (dices[i].value == 0) {
$('td div:nth-child(' + i + ')').addClass("zero-desktop")
} else if (dices[i].value == 1) {
$('td div:nth-child(' + i + ')').addClass("one-desktop")
} else if (dices[i].value == 2){
$('td div:nth-child(' + i + ')').addClass("two-desktop")
} else if (dices[i].value == 3) {
$('td div:nth-child(' + i + ')').addClass("three-desktop")
} else if (dices[i].value == 4) {
$('td div:nth-child(' + i + ')').addClass("four-desktop")
} else {
alert ("NOT WORKING")
}
}

JS Empty focused Input

If the user enters an invalid number, I want to empty the input. Negative numbers and numbers that are decimal are not allowed.
This is my Js Function:
function calcPrice() {
var count = $('#placeCount').val();
if(count%1 == 0 && count > 0) {
if(count == 0) {
var output = "";
} else if(count == 1) {
var output = "€ 2,49";
} else if(count > 1 && count != 0) {
var output = ("€ " + (2*count-0.01));
}
}
else {
$('#placeCount').html("");
}
$('#priceOutput').html(output);
}
But somehow the input is not empty if I enter a count that goes into the else section.
change the value of the input with val() instead of html():
function calcPrice() {
var count = $('#placeCount').val();
if(count%1 == 0 && count > 0) {
if(count == 0) {
var output = "";
} else if(count == 1) {
var output = "€ 2,49";
} else if(count > 1 && count != 0) {
var output = ("€ " + (2*count-0.01));
}
}
else {
$('#placeCount').val("");
}
$('#priceOutput').html(output);
}

Except link with Javascript

can you help me?
I'm trying to get this code below to make a excesão links in case the variable would be
var except_link = "blogspot.com, wordpress.com";
But it is only making excesão of 'blogspot.com'
thank you
var except_link = "blogspot.com, wordpress.com";
var allow_file = ".3gp,.7z,.aac,.ac3,.asf,.asx,.avi,.bzip,.divx,.doc,.exe,.flv,.jpg,.png,.gz,.gzip,.iso,.jar,.jav,.mid,.mkv,.mov,.mp3,.mp4,.mpeg,.mpg,.msi,.nrg,.ogg,.pdf,.ppt,.psd,.rar,.rm,.rmvb,.rtf,.swf,.tar,.tar.gz,.tgz,.torrent,.ttf,.txt,.wav,.wma,.wmv,.xls,.zip,180upload,1fichier,1filesharing,2shared,4files,4share,4shared,a2zupload,adf,adrive,adv,amazo,";
except_link = (link_except != null) ? link_except : except_link;
function check(siteurl) {
if ("" + allow_file != "undefined" && allow_file != "" && allow_file.replace(/\s/g, "") != "" && siteurl != "") {
if ((" " + allow_file).indexOf(",") > 0) {
pular = allow_file.split(",")
} else {
pular = new Array(allow_file)
}
for (s = 0; s < pular.length; s++) {
if ((" " + siteurl.toLowerCase()).indexOf(pular[s].toLowerCase()) > 0) {
if ("" + except_link != "undefined" && except_link != "" && except_link.replace(/\s/g, "") != "" && siteurl != "") {
if ((" " + except_link).indexOf(",") > 0) {
pular = except_link.split(",")
} else {
pular = new Array(except_link)
}
for (s = 0; s < pular.length; s++) {
if ((" " + siteurl.toLowerCase()).indexOf(pular[s].toLowerCase()) > 0) {
return false;
break
}
}
return true
} else {
return true
}
}
}
return false
} else {
return false
}
}
You're splitting based on "," where you are delimiting with ", " (comma followed by space). The problem could be that it doesn't match since the second URL in the split array will be " wordpress.com" (wordpress.com with a space in front of it).
To remedy this, you could either change your split function to split accordingly:
except_link = "blogspot.com, wordpress.com" // comma-space
...
except_link.split(", ") // comma-space
Or, change your input string to accommodate your split requirements:
except_link = "blogspot.com,wordpress.com" // comma, no space
...
except_link.split(",") // comma, no space

Nothing happens after Prompt

Sorry, I wasn't clear enough. I need it to list all the numbers from 0 to the number inputted by the prompt into the HTML. I made some suggested changes but now I only get the result for the specific number inputted, not all the numbers up to that number. I am just starting out so please be gentle. Thanks!
$(function() {
var number = parseInt(prompt("Let me see a number:"));
var result;
for(var i = 0; i <= number; i++) {
if ( i %15 == 0) {
result = "Ping-Pong";
}
else if (i %5 == 0) {
result = "Pong";
}
else if (i %3 == 0) {
result = "Ping";
}
else {
result = number;
}
document.getElementById("show").innerHTML = result;
};
});
You can do either:
for(var i = 0; i <= number; i++) {
var digit = number[i]; // or any other assigment to new digit var
if ( digit % 5 == 0) {
return "Ping-Pong";
}
.... rest of your code here.
or
if ( number % 5 == 0) {
return "Ping-Pong";
}
.... rest of your code here.
Problem is you did nothing after the return keyword. Also you didn't declared variable as digit. I hope this is what you are looking for.
With loop:
$(function() {
var number = parseInt(prompt("Let me see a number:"));
var result;
for (var i = 0; i <= number; i++) {
if (i % 15 == 0) { // replaced `digit` with `i`
result = "Ping-Pong";
} else if (i % 5 == 0) {
result = "Pong";
} else if (i % 3 == 0) {
result = "Ping";
} else {
result = number;
}
alert(result);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Without loop:
$(function() {
var number = parseInt(prompt("Let me see a number:"));
var result;
if (number % 15 == 0) { // replaced `digit` with `number`
result = "Ping-Pong";
} else if (number % 5 == 0) {
result = "Pong";
} else if (number % 3 == 0) {
result = "Ping";
} else {
result = number;
}
alert(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Ok, I figured it out. For future reference, this is what I was trying to do:
$(function() {
var number = parseInt(prompt("Let me see a number:"));
var i
var text = "";
for(i = 1; i <= number; i++) {
if ( i %15 == 0) {
text += "<br>" + "Ping Pong" + "<br>";
}
else if (i %5 == 0) {
text += "<br>" + "Pong" + "<br>";
}
else if (i %3 == 0) {
text += "<br>" + "Ping" + "<br>";
}
else {
text += "<br>" + i + "<br>";
}
};
document.getElementById("show").innerHTML = text;
});

NaN error equation

In this javascript code I try to solve a quadratic equation, I've been working on it for an hour and this should tell me the value of a, b and c where y is a(x^2). I'm a relative javascript beginner and would love some help. Why are the values of a, b and c not numbers? The variable names are in italian, in english and in something else(Not even I know what), but I commented what they are. That's one of my bad traits as a student that usually works alone, sorry if it's not easy to understand.
<script type="text/javascript">
var equa=prompt("Scrivi l'equazione senza spazi usando x come incognita e y come x^2");
var a = 0.0; b = 0.0; c = 0.0;//ax2+bx+c
var prima = true; //before or after equal?
var ope = 1;//1add 2sub 3mul 4div
var lasto = 0.0;//last number, used for div and mul
var lastos = 3; //1a2b3c
var errore=0;//is something messed up?
for(var i = 0; i < equa.length;i=i){
if(equa.charAt(i)=='='){
prima = false;
i++;
}else if(equa.charAt(i)=='+'){
ope=1;
i++;
}else if(equa.charAt(i)=='-'){
ope=2;
i++;
}else if(equa.charAt(i)=='*'){
ope=3;
i++;
}else if(equa.charAt(i)=='/'){
ope=4;
i++;
}else{
var nume = "";//Current number in string form
while(i<equa.length && equa.charAt(i)>'0' && equa.charAt(i)<'9'){
nume+=equa.charAt(i);
i++;
}
var lasnum = 0.0;//current number in float form
var lasval = 3; //1a2b3c
if(equa.charAt(i) == 'x'){
lasnum=parseFloat(nume);
lasval = 2;
}else if(equa.charAt(i) == 'y'){
lasnum=parseFloat(nume);
lasval = 1;
}else{
lasnum = parseFloat(nume);
lasval=3;
}
i++;
if( (ope == 1 || ope == 2) && !(equa.charAt(i) =='*' || equa.charAt(i) == '/')){
if(lasval == 1){
if(prima) a+=lasnum;
else a-=lasnum;
}
else if(lasval == 2){
if(prima) b+=lasnum;
else b-=lasnum;
}
else {
if(prima) c+=lasnum;
else c-=lasnum;
}
}else if( (ope==1 || ope == 2) && (equa.charAt(i) =='*' || equa.charAt(i) == '/')){
//zitto e fermo
lasto=lasnum;
lastos=lasval;
}else if( (ope==3||ope == 4)){
if(ope==3){
if(lastos==3){
lasnum*=lasto;
}
if(lastos == 2){
if(lastval==3){
lasnum*=lasto;
lastval=2;
}
if(lastval==2){
lasnum*=lasto;
lastval=1;
}
if(lastval==1){
errore=1;
}
}
if(lastos == 1){
if(lastval == 3){
lasnum*=lasto;
lastval=1;
}else{
errore=1;
}
}
}
if(ope == 4){
if(lastos == 1){
if(lastval==3){
lasnum/=lasto;
lastval=1;
}
if(lastval==2){
lasnum/=lasto;
lastval=2;
}
if(lastval==1){
lasnum/=lasto;
lastval=3;
}
}
if(lastos == 2){
if(lastval==1){
errore=1;
}
if(lastval==2){
lasnum/=lasto;
lastval=3;
}
if(lastval==3){
lasnum/=lasto;
lastval=2;
}
}
if(lastos == 3){
if(lastval==3){
lasnum/=lasto;
}else{
errore=1;
}
}
}
if(equa.charAt(i) =='*' || equa.charAt(i) == '/'){
lasto=lasnum;
lasto=lasval;
}else{
if(lasval == 1){
if(prima) a+=lasnum;
else a-=lasnum;
}
else if(lasval == 2){
if(prima) b+=lasnum;
else b-=lasnum;
}
else {
if(prima) c+=lasnum;
else c-=lasnum;
}
lasto=0;
lastos=3;
}
}
}
}
if(errore==0){
alert("a ="+a+" b="+b+" c="+c);
}else{
alert("AOOOOOOo");
}
</script>
Since the expected input should be in the format "15y+3x+5=20" for example, then this is a simple regular expression:
var equa = prompt("Scrivi l'equazione senza spazi usando x come incognita e y come x^2");
var regex = /^([0-9.]+)y\+([0-9.]+)x\+([0-9.]+)=([0-9.]+)$/;
var matches = regex.exec(equa);
if (matches) {
var a = parseFloat(matches[1]);
var b = parseFloat(matches[2]);
var c = parseFloat(matches[3]) - parseFloat(matches[4]);
var discriminant = b*b - 4*a*c;
if (discriminant < 0) {
alert('No real solutions');
}
else {
var root = Math.sqrt(discriminant);
alert('Root: ' + ((-b + root)/(2*a)) + ', ' + ((-b - root)/(2*a)));
}
}
else {
alert("AOOOOOOo");
}

Categories

Resources