I can’t explain why I used the code i did. I don’t understand any of this so i just keep throwing in answers. Can someone please explain this? How do i make my brain think like this?
Hello @byte5723385929. One way to complete the task-printing series if odd numbers would be to use console.log at every line-this therefore prints everything you need.
The first two-console.log(number) is correct, since number is 5, and therefore odd.
The next odd number down from five is three, hence the (3) on the third line. What the question wants you to do is to print that value-3. How would you print a number to the console?
The last line requires you to print the next odd number down from 3, which is 1. The first part console.log is correct, since it logs the result. The next part requires you to get the number 1 from the value of the variable number. Since number is 5, you need to subtract something from 5 to get 1-which is 4: 5-4=1. I assume, that 4 was not an option, and you need to therefore subtract multiple values. In this case, subtracting 5-3-1 gives 4, since 5-3=2 and 2-1=1.
+ this adds two values
- this subtracts two values
* this multiplies two values
/ this divides
% finds the remainder of dividing two values
+= adds the value on the right to the value on the left
-= subtracts the value on the right from the value on the left
*= multiplies the value on the right with the value on the left
/= divides the value on the value on the left with the value on the right
%= sets something equal to the remainder of dividing the value on the left
with the value on the right
++ adds one to the value on the left
-- subtracts one from the value on the left
To find out more about mathematical operators, this is a good read.
I hope this helps!
Thank you for the reply. I understood (for once) what it wanted me to do…display 5, 3, 1on the console. What i don’t get is how to do that with the options…i should have included those in the screencap. I don’t understand this though:
If we already printed 5 and 3, why subtract 4 and not 2? Does the number reset to 5 each time?
Thank you for the reference on operators, I remember learning about them but i don’t see how it works now because I don’t understand what this code is doing besides subtracting numbers…in some weird way.
If you are trying to log 1, since number is equal to 5, don’t you think you should log number -4? If that is not an option “subtract” 3 first from number to get 2, then “subtract” 1 again to get 1, hence number-3-1.
The reason is that you never subtract anything from the numbers variable-you just print various numbers. In fact, since the variable is const, you can never reassign it.
const number = 5;//declares a variable with a value of 5
//since it is a const variable, it cannot be reassigned
console.log(number);//logs number-no change to the 'number' variable
console.log(3);//logs 3 to the console-no change to number
console.log(number-3-1);//logs the result of subtracting the value stored
//inside the number variable: 5 - 3 - 1. Since JS follows the order of
//operations, 5-3 is evaluated first, giving 2, then 2-1 is evaluated
//giving 1. There is still no change to the number variable.
I’m not really sure what you’re trying to say, but are you asking why after “subtracting” 3 from number the value is still 5?
If so, it’s because:
number is declared with const so it can’t be changed.
You are not directly reassigning or changing number. All you are doing is referencing the value stored in that variable.
Also why would you subtract 2 and 1 from 5? That gives you 2, but they want 5, 3, and 1 logged.
Also this statement: console.log(number-3-1); first references the value from number(5), and subtracts 3 and 1 from it to get 1, which it then logs.
Ok, seems you are a little confused.
Look at this code:
const number = 9;
Let’s say I wanted to print out 6:
const number = 9;
This references the value in number, which is 9, and logs that value - 3 which logs 6, because 9-3 is 6 right?
However when you go to log number it will still print out 9, because even though you are logging number - 3, you are not changing number in ANY way.
When you complete line three, what you should actually be doing is purely console.log(3). The value stored in number does not change, and is therefore still 5. So in order to get from 5 to 1-as 1 is the next odd number down from 3, you must subtract 4 from 5. If -4 is not an option, you can use other number combinations, such as 5-3-1: