0

For each number between 1 and 100...

If the number is odd, print it out. But if the number is even, print the word "Steven" instead.

When we run the program, we should see the following output:

1
Steven
3
Steven
5
Steven

...

97
Steven
99
Steven

The program needs to start at 1 (not 0) and the number 100 must be included - the last number should be 100, not 99.

I'm still learning Javascript, so my first instinct is to create an While Statement:

var Number
var Counter = 1;

While (Number == even
       Number <= 100) {
    print ("Steven");
} else {
    print (Number);

Should this even be a while loop though? How can I fix this statement so that it prints out odd numbers and "Steven" in-place of even numbers?

Edit: Tried to run in JSFiddle but nothing happened.

user3386109
  • 34,287
  • 7
  • 49
  • 68
HappyHands31
  • 4,001
  • 17
  • 59
  • 109
  • This can be done in any language you want. Try it and see. – aynber Oct 07 '16 at 18:16
  • 1
    It would be a lot easier with a for loop since you are running for a fixed length of consecutive numbers. Also, be careful of your capitalization since javascript is case sensitive. It's safest to make all your variables lowercase. – raphael75 Oct 07 '16 at 18:16
  • 5
    *"Should I be coding in JS or should I be using C or PHP?"* Umm yeah, that's the first thing ***you*** need to decide. Let us know what you decide. – user3386109 Oct 07 '16 at 18:17
  • Possible duplicate of [Testing whether a value is odd or even](http://stackoverflow.com/questions/6211613/testing-whether-a-value-is-odd-or-even) – Mehrad Oct 07 '16 at 18:18
  • 1
    Also, be careful about using a reserved word as a variable name. If you do that you'll get an error. In javascript, Number (capital N) is a built-in object, so you shouldn't use that as a variable name. – raphael75 Oct 07 '16 at 18:18
  • 1
    You should check out a js reference for help with this. Here is a good one: http://www.javascriptkit.com/jsref/looping.shtml – raphael75 Oct 07 '16 at 18:20
  • 1
    Alright I'm using Javascript then because that's the language I'm working on. I tried out what I had in JS Fiddle - nothing happened. – HappyHands31 Oct 07 '16 at 18:21
  • 1
    The [info page for javascript](https://stackoverflow.com/tags/javascript/info) has a list of resources that should help (scroll down to the *"Learning JavaScript"* section). – user3386109 Oct 07 '16 at 18:39

2 Answers2

2

In JavaScript - Use a for loop and loop through 1 - 100. Check the remainder when dividing by two. If the remainder is 0, we know it's even, so console.log "Steven", otherwise the current number.

for (i = 1; i <= 100; i++){
  if (i % 2 == 0) console.log("Steven")
  else console.log(i)
}
larz
  • 5,724
  • 2
  • 11
  • 20
  • Thanks for your answer. Why is it that when we run the code snippet, the first output is 51 (instead of 1)? – HappyHands31 Oct 07 '16 at 18:54
  • 1
    I think StackOverflow has a limit on the number of lines of console it will show. – larz Oct 07 '16 at 19:01
  • You can also increment by 2 instead of 1 in your for loop. Then you can print `i` and "Steven" every time and eliminate the even check. – Don't Panic Oct 10 '16 at 17:52
1

Using the modulus operator should do the job

Using PHP:

for ($i=1; $i < 100; $i++):
   if ($i % 2 == 0) {
       echo "Stephen";
    }
    else {
       echo $i;
    }
endfor;