New Posts  All Forums:Forum Nav:

javascript help

post #1 of 10
Thread Starter 
hey everyone im taking a web programming class and im just starting javascript , i was hoping someone could help me.

i have to prompt the user to enter a start number and an end number than display the numbers , but with the odd numbers in red and the evens in black.using a loop.

so far i have , i cant figure out how to select the odd numbers and change the color.

var x = Number (prompt("enter start number",3));
var y = Number (prompt("enter end number",50));

do

{
document.write(x + "
");
x++;


}

while(x < y);
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
post #2 of 10
Modulus division is what you are looking for I think. It'll give you the remainder. So for example 50 % 2 = 0 because 50 / 2 leaves no remainder, where 3 % 2 = 1.

So I'd throw your two variables in array then loop through the array, checking if the number in the array leaves a remainder if it does output in red else output in black.

Hope that makes sense.
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
post #3 of 10
Thread Starter 
i havent learned arrays yet , is there a way i can do it with just a loop and variables ?.

i understand the modulus division thing (x%2==0) would be even and x%2!=0) would be odd. but where would the correct place to put these in my loop.
i know this is alot to ask but could code this for me , ive spent all day doing this and im literally pulling my hair out.
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
post #4 of 10
Thread Starter 
and would i use document.fgColor = "red" to change the color?
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
post #5 of 10
Quote:
Originally Posted by jordanjr23 View Post

i havent learned arrays yet , is there a way i can do it with just a loop and variables ?.

i understand the modulus division thing (x%2==0) would be even and x%2!=0) would be odd. but where would the correct place to put these in my loop.
i know this is alot to ask but could code this for me , ive spent all day doing this and im literally pulling my hair out.

I don't think you can loop through a bunch of variables, at least I wouldn't know how to do it. Arrays aren't all to complicated, I'll show you.

Quote:
Originally Posted by jordanjr23 View Post

and would i use document.fgColor = "red" to change the color?

Normally you'd want to have an external stylesheet somewhere, but for a small thing like this I would do this to change a color
Code:
document.write("<p style='color: red'>Text in here will be red</p>");
Code:
var x = Number (prompt("enter start number",3));
var y = Number (prompt("enter end number",50));

//Once you have your numbers make an array and fill the first to spots with your input from the user
var numbers = new Array();
numbers[0] = x;
numbers[1] = y;

//Now make another array to hold the letter in string format
var letters = new Array();
letters[0] = 'x';
letters[1] = 'y';

//So loop through the array incrementing i until i is no longer less then the length of the array
for(i = 0; i < numbers.length; i++)
{
        //if the number in the array isn't even
        if(numbers[i] % 2 != 0)
        {
                 //So output the red text as I showed above, output the string held in the letters array then output the number in the numbers array
                //Think of letters[i] being letters[0] or letters[1] depending on what i is equal to, which gives it whatever value is held in that spot in the array.
                //The above goes for numbers[i] as well
                document.write("<p style='color: red'>" + letters[i] + " is " + numbers[i] + "</p>");
        }
        //If it is even
        else
        {
                document.write("<p>" + letters[i] + " is " + numbers[i] + "</p>");
        }
}

I hope that all makes sense, I tried to keep it simple.
Edited by the_dude - 3/25/13 at 3:17pm
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
post #6 of 10
Thread Starter 
thanks for your help but i was just trying to write out the numbers between what numbers the user specified like 3 4 5 6 7 8 9 10 and color odd ones red and the even black. no text neccessary
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
post #7 of 10
Quote:
Originally Posted by jordanjr23 View Post

thanks for your help but i was just trying to write out the numbers between what numbers the user specified like 3 4 5 6 7 8 9 10 and color odd ones red and the even black. no text neccessary

What do you mean no text necessary? How would you know which ones are coloured?
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
post #8 of 10
Thread Starter 
i meant only numbers sorry
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
post #9 of 10
Code:
var x = Number (prompt("enter start number",3));
var y = Number (prompt("enter end number",50));

for(i = x; i <= y; i++)
{
        if(i % 2 != 0)
        {
                document.write("<p style='color: red'>" + i + "</p>");
        }
        else
        {
                document.write("<p>" + i + "</p>");
        }
}
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ Stock Asus P6T ATI HD5870 OCZ 6GB 1333mhz 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Caviar Black 640GB LG DVD Drive Cm Hyper 212+ 
CoolingOSMonitorPower
Arctic Cooling Accelero Twin Turbo II Windows 7 64bit ASUS VW246H Corsair HX850 
Case
CM 690 II Adavanced 
  hide details  
Reply
post #10 of 10
Thread Starter 
seriously dude i love you , thank you so much for helping me.
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
switch
(13 items)
 
  
CPUMotherboardGraphicsHard Drive
3770k asus p8z77-lk AMD Radeon HD 7950 adata s510  
Hard DriveHard DriveCoolingMonitor
spinpoint caviar blue noctua nh d 14 x-star 1440p 96hz 
KeyboardPowerCaseMouse
filco majestouch 2 antec neo eco 620w nzxt switch 810 razer deathadder 
Mouse Pad
steelseries qck+ 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding