Let's tackle this challenge:
Your task is to create a program that adds up all the odd numbers between 1 and the number provided by the user. For instance, if the user inputs 7, the program should calculate 1 + 3 + 5 + 7. The result of this calculation, along with the expression used, should be displayed on the screen. In this case, the answer would be 16.
This is what I have so far:
//Setting up the required variables
var num = prompt("Please enter a number");
var sum = 1;
var currentNum = 1;
var operator = "+";
//Calculating the sum
document.write("The total sum of all odd numbers is: ");
do {
if(num % 2 == 0) {
}
else{
document.write(currentNum + operator);
currentNum = currentNum + 1;
sum = sum + currentNum;
}
}while(currentNum < num);
document.write(num + " equals " + sum);