Change Marquee Content Using Javascript - javascript

Before anyone says, I know there are better ways than using a marquee, however, for this instance I am using one.
Depending on the date, I want the marquee to say a different thing. Why is the marquee not changing and always saying default?
Javascript
var d = new Date();
var n = d.getDate();
if (n > 0 && n < 8){
var bday ="Birthday Kids name and age 1"
}else if(n > 7 && n < 15){
var bday ="Birthday Kids name and age 2"
}else if(n > 14 && n < 22){
var bday ="Birthday Kids name and age 3"
}else if(n > 21 && n < 29){
var bday ="Birthday Kids name and age 4"
}else if(n > 28 && n < 32){
var bday ="BirthdayKids name and age 5"
}
document.getElementById("birthdays").textContent = "We wish a very happy birthday to "+bday;
<marquee bgcolor="#088A08" id="birthdays" direction="left" loop="20" width="100%">Default</marquee>

See the fiddle
Javascript
var d = new Date();
var n = d.getDate();
if (n > 0 && n < 8){
var bday ="Birthday Kids name and age 1";
}else if(n > 7 && n < 15){
var bday ="Birthday Kids name and age 2";
}else if(n > 14 && n < 22){
var bday ="Birthday Kids name and age 3";
}else if(n > 21 && n < 29){
var bday ="Birthday Kids name and age 4";
}else if(n > 28 && n < 32){
var bday ="BirthdayKids name and age 5";
}
document.getElementById("birthdays").textContent ="We wish a very happy birthday to "+bday;
Notice that i've removed the == from your code which solves the problem as there is a problem with the syntax error.
== is used for comparisons and for assignments just = is used..

try
document.getElementById("birthdays").innerHTML
edit:
replace == with = and make sure the getElementById is called after the element or after the page is loaded

Related

How to code else if in short if statement? [duplicate]

This question already has answers here:
shortHand if else-if and else statement
(3 answers)
Closed 3 years ago.
For example, I have code with short if statement and how can I code else if in this code?
var age = 16;
age > 18 ? console.log("> 18") : console.log("< 18");
That would work like this code
var age = 16;
if (age > 18){
console.log("> 18");
}else if(age == 18){
console.log("= 18");
}else{
console.log("< 18");
}
A hybrid approach is recommended in this case:
var age = 16;
if (age == 18) {
console.log("= 18");
} else {
console.log(age > 18 ? "> 18" : "< 18");
}
Do it with ternary expression,
var age = 16;
console.log(age === 18 ? "=18" : age < 18 ? "< 18" : "> 18")
var age = 19;
console.log(age === 18 ? "=18" : age < 18 ? "< 18" : "> 18")
var age = 18;
console.log(age === 18 ? "=18" : age < 18 ? "< 18" : "> 18")
The if way could also be written like this:
var age = 16;
if (age > 18) {
console.log('> 18')
} else if (age == 18) {
console.log("= 18");
} else {
console.log("< 18");
}
So, you could use a nested ternary in the style of the code above:
var age = 16;
age > 18 ? console.log('> 18') : age === 18 ? console.log("18") : console.log("< 18")
Note, it would be more efficient to write the ternary inside console.log, since you are always returning similar values (in this case, strings):
var age = 16;
console.log(age > 18 ? age === 18 ? '18' : '< 18' : '< 18')

If number are equal and even pop an alert

I have few problems with JavaScript. I don't know why it's not working. I've searched the internet but didn't find anything.
I need to popup an alert if both number are equal to popup number (if they are equal) if not, an alert with message (Please insert numbers). But I can't make it work.
function even(){
var n = parseInt(document.getElementById("n").value);
var m = parseInt(document.getElementById("m").value);
var s = 0 ;
var i;
if (n < m) {
i = n;
while (i <= m){
if(i % 2 === 0)
s += i;
i++;
}
alert(s);
}
else if (n > m) {
i = m;
while (i <= n) {
if (i % 2 === 0)
s += i;
i++;
}
alert(s);
}
else if (n = m) {
i = m;
i = n;
while(i % 2 == 0)
s == i;
}
alert(s);
}
}
<input type="text" id="n" > </br><br>
<input type="text" id="m" > </br><br>
<button type="button" onclick="even()">Sum Even Numbers</button>
function even(){
var n = parseInt(document.getElementById("n").value);
var m = parseInt(document.getElementById("m").value);
if(n % 2 == 0 && m % 2 == 0){
alert(n + m);
}
}
This is way simpler and does work only if n & m are even numbers. Thanks to n % 2 == 0 and m % 2 == 0 which divide the numbers by to and check if the remainder is equal to 0.
EDIT
But reading your question again I'm not sure it is you want to achieve. You might want to be more precise.
Based on the code you have above, I assume goal is to sum the even numbers between n and m. Something like this? Perhaps,
function even() {
var n = parseInt(document.getElementById("n").value);
var m = parseInt(document.getElementById("m").value);
var s = 0;
var start = Math.min(n, m);
var end = Math.max(n, m);
for (var i = start; i <= end; ++i) {
if (i % 2 === 0) {
s += i;
}
}
alert(s);
}
jsFiddle
Notes
while(i%2 == 0) s == i; should probably be while(i % 2 == 0) { s = i; }
}else if(n=m) { should probably be } else if (n === m) {
I used Math.min() and Math.max() since it seemed like the first two sections of your if-statement did the same thing but with n and m swapped.

Calculating unit price based on quantity entered

I'm trying to create a script that displays a price in one box based on the number of employees entered in an adjacent box.
The script works fine for a static amount, i.e. the base price is £8 per employee. If 5 is entered the price displayed is correct, £40.
The problem I'm having is changing the unit price when a certain threshold is met. The unit price for 1-5 employees is 8, 6-21 is 7 and 22-50 is 6.
No matter what is entered in the employee box, the number is always multiplied by 8.
I'm very inexperienced when it comes to javascript so any help will be very welcome!
Here is my code:
<div class="employee-button">
<form name="calculator" id="calculator">
<input name="employees" id="employees" type="text" placeholder="# of Employees" />
</form>
</div>
<div class="cost-button">
<span id="result">Monthly Cost</span>
</div>
Javascript
$(function() {
var ccNum;
$('#employees').keyup(function(){
ccNum = $(this).val();
});
if((ccNum >= 0) && (ccNum <= 5)){
var unitPrice = 8
}
else if((ccNum >= 6) && (ccNum <= 21)){
var unitPrice = 7
}
else if((ccNum >= 21) && (ccNum <= 50)){
var unitPrice = 6
}
else var unitPrice = 8;
$('#employees').keyup(function(){
$('#result').text(ccNum * unitPrice);
});
})
Put everything within the keyup function. Your code wasn't working because the logic to calculate the price is only executed once, not for every keyup event.
$('#employees').keyup(function(){
var ccNum = $(this).val();
if((ccNum >= 0) && (ccNum <= 5)){
var unitPrice = 8
}
else if((ccNum >= 6) && (ccNum <= 21)){
var unitPrice = 7
}
else if((ccNum >= 21) && (ccNum <= 50)){
var unitPrice = 6
}
else var unitPrice = 8;
$('#result').text(ccNum * unitPrice);
});
In addition to putting the code inside your event handler, you do not need to keep declaring the same variable over and over. Also the test for 0 to 5 is not needed (as already covered by the default value):
$('#employees').keyup(function(){
var ccNum = ~~$(this).val();
// Start with the default
var unitPrice = 8;
if((ccNum >= 6) && (ccNum <= 21)){
unitPrice = 7;
}
else if((ccNum >= 21) && (ccNum <= 50)){
unitPrice = 6;
}
$('#result').text(ccNum * unitPrice);
});
Note: ~~ is a shortcut conversion to an integer value.

Basic JavaScript crashes website

Im having problems finishing the codeavengers practice tutorial
when i run this code it just crashes, any idea why?
The purpose is to count how many teenagers from the range of 13-19
var age1 = prompt('Please insert your age');
var age2 = prompt('Please insert your age');
var count = 0;
if (age1 > 12 && age1 < 20 || age2 > 12 && age2 < 20) {
count = count + 1;
}
alert(count);
there is nothing that will stop the while loop! age1 and age2 are static values that are never changing. Also don't forget the semicolins on lines 1 and 2

Error in JavaScript return code?

Here is the javascript code:
There is an error in code where nightSurcharges is added to total cost even if pickUptime is less than 20.
function TaxiFare() {
var baseFare = 2;
var costPerMile = 0.50;
var nightSurcharge = 0.50; // 8pm to 6am, every night //its flat 0.50 and not per mile
var milesTravelled = Number(document.getElementById("miles").value) || 0;
if ((milesTravelled < 1) || (milesTravelled > 200)) {
alert("You must enter 1 - 200 miles");
document.getElementById("miles").focus();
return false;
}
var pickupTime = Number(document.getElementById("putime").value) || 0;
if ((pickupTime == "") || (pickupTime < 0) || (pickupTime > 23)) {
alert("The time must be 0-23 hours");
document.getElementById("putime").focus();
return false;
}
var cost = baseFare + (costPerMile * milesTravelled);
// add the nightSurcharge to the cost if it is after
// 8pm or before 6am
if (pickupTime >= 20 || pickupTime < 6) {
cost += nightSurcharge;
}
alert("Your taxi fare is $" + cost.toFixed(2));
}
I want nightSurcharge to be added only when pickupTime is >=20, but that's not working right now.
Any help is appreciated. Thanks
This seems obvious to me.
if (pickupTime >= 20 || pickupTime < 6) {
cost += nightSurcharge;
}
This code right here adds nightSurcharge to the cost if pickupTime is greater than or equal to 20, OR less than 6. So of course it's added if it's less than 6.
if (pickupTime >= 20) {
cost += nightSurcharge;
}
Now it will only add to it if it's greater or equal to 20.
your code is:
if (pickupTime >= 20 || pickupTime < 6)
so if pickupTime is less then 6 it'll enter the if as well
http://jsfiddle.net/7rdzC/

Categories

Resources