Beginner - While Loop sum all user inputs under 50 - Javascript - javascript

I am still a beginner on Javascript and I have a question.
I would like to sum all user inputs under 50 if written 0 to stop the program and display their amount
By example:
First Number 5, Second number 3, Third Number 55, Fourth Number 0. (The program will print 8)
var userInput = parseInt(prompt('Write Number'));
while(userInput!=0) {
var userInput = parseInt(prompt('Try Again!'));
if(userInput < 50){
var sum = userInput + userInput;
}
document.write(sum + '<br>');
}
Thanks a lot

loop and add previous value if condition match :
var inputData = Number(prompt("Enter the input number"));
var finalOutput=0;
while (inputData > 0) {
if (!(inputData > 50)) {
finalOutput += inputData;
}
inputData = Number(prompt("Enter the input number"));
}
document.write("SUM is : " + finalOutput);

You'd do this:
var input = Number(prompt("Enter a number"));
var output = 0;
while (input > 0) {
if (!(input > 50)) {
output += input;
}
input = Number(prompt("Enter a number"));
}
document.write("The sum of all the numbers lower than 50 was " + output);

Related

Multiplying Combinations Array

So I need a tiny bit of help with this code, Some background information: The user inputs a number, the code takes the number and outputs various combinations of numbers that multiply to it.
For example:
Input: 7
Output: (1,7)(7,1).
*But what really happens:
*
Input: 7
Output: (7,1)
I want my code to reverse the numbers as well, so it makes can look like it has two combinations
var input= parseInt(prompt("Please enter a number larger than 1"));
var arr = [];
if(input <= 1) {
console.log("Goodbye!")
}
while(input > 0) {
var arr = [];
var input = parseInt(prompt("Please enter a number larger than 1"));
for (var i = 0; i < input; ++input) {
var r = ((input / i) % 1 === 0) ? (input / i) : Infinity
if(isFinite(r)) {
arr.unshift(r + ", " + i)
}
}
console.log("The multiplicative combination(s) are: " + "(" + arr.join("), (") + "). ");
}
My code just need this tiny bit of problem fixed and the rest will be fine!
Your code has 2 infinite loop because you never change i and always increase input.
also in this line for (var i = 0; i < input; ++input) you never let i to be equal to the input so in your example (input=7) you can not have (7,1) as one of your answers. I think this is what you looking for:
var input = 1;
while(input > 0) {
input = parseInt(prompt("Please enter a number larger than 1"));
if(input > 1) {
var arr = [];
for (var i = 0; i <= input; ++i) {
var r = ((input / i) % 1 === 0) ? (input / i) : Infinity
if(isFinite(r)) {
arr.unshift(r + ", " + i)
}
}
console.log("The multiplicative combination(s) are: " + "(" + arr.join("), (") + "). ");
continue;
}
else{
console.log("Goodbye!");
break;
}
}

In javascript how to make multiplication table to be generated by default of 5 or any other number, if the user enters no input number?

Write a program to take input a number from user &
display it’s multiplication table on your browser. If user
does not enter a new number, multiplication table of 5
should be displayed by default.
And this is how I want it to be generated with by default table of 5 encoded,
var num = prompt("Enter Number", "0") //prompt user to enter the number
var num = parseInt(num); //parse the num to number
var i = 0;
document.write('<table border="1" cellspacing="0">');
for (i = 1; i < 10; i++) {
document.write("<tr><td>" + num + " x " + i + " = " + num * i + "</td></tr>");
}
This will help you with your problem. It works like this. if parseInt(num) is a truthy(not 0) it'll use parseInt(num) else if it is falsy(0) then it'll use 5
var num = prompt("Enter Number", "0") //prompt user to enter the number
var num = parseInt(num) || 5;

Calculate the sum of positive values smaller or equal to a number

I am trying to calculate the sum of positive values smaller or equal to the entered number, for ex: 5 -> 1+2+3+4+5 = 15
I came up with this:
var num = Number(prompt("Enter a number "));
sum = 0;
i = num;
do {
sum = sum += i;
i--
document.write(sum);
} while (i > 0);
I don't understand what I am doing wrong.
i think this is correct code:
var num = Number(prompt("Enter a number "));
sum = 0;
i = num;
do
{
sum += i;
i--;
}
while (i > 0);
document.write(sum);
and i suggest you to use this formula : document.write((num * (num + 1)) / 2);
If you look closer to your task, you'll find out, that:
If Num = 1, the sequence to be summed is [1]
if Num = 2, the sequence is [1, 2]
if Num = 3, the sequence is [1, 2, 3]
You can imagine, that you have a square with sides equal to num, for example, when num = 4:
****
****
****
****
And you need to summ 1, 2, 3, 4:
***#
**##
*###
####
See? It's a square of a triangle.
It could be calculated by formula: num * (num + 1) / 2
So, you code could be:
var num = Number(prompt("Enter a number "));
document.write(num * (num + 1) / 2)
You are writing the sum on each loop instead you have to print it finally. If you want to print the numbers then keep it an array and join them with + symbol before writing. To make it in ascending order change the loop condition.
var num = Number(prompt("Enter a number "));
sum = 0;
i = 1;
nums = [];
do {
sum = sum += i;
nums.push(i++);
}
while (i <= num);
document.write(nums.join(' + ') + ' = ' + sum);
Do with increment instead of decrements.And also show result of sum outside of loop .Not with in loop.And create array to append increment value.Finally print with document.write
var num=Number(prompt("Enter a number "));
sum = 0;
i = 1;
var a=[];
do {
sum +=i;
a.push(i)
i++;
}
while (num >= i);
document.write(a.join('+')+'='+sum)
You should write the answer at the end of loop and make this simple sum += i;.
var num = Number(prompt("Enter a number"));
sum = 0;
i = num;
do {
sum += i;
i--;
}
while (i > 0);
document.write(sum);
var number = 5, // Your number
result = 0;
while ( number !== 0 ) {
result += number;
number--;
}
document.write(result);
Fast and precious solution.
Here is the case with complete check and display as you need: JAVA
public static void main ( String arg[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Number entered : " + number);
int sum =1 ;
if(number > 1) {
int nextNumber = 1;
System.out.print(nextNumber);
do {
// sum of all the positive numbers
nextNumber++ ;
sum = nextNumber + sum;
System.out.print( " + " + nextNumber);
}while(nextNumber < number);
System.out.print(" = " + sum);
}
}
var num = Number(prompt("Enter a number"));
sum = 0;
for (i = num; i > 0; i--) {
sum += i;
}
document.write(sum);

Javascript find average from user's input

I know there are similar questions on here to mine but I don't see the answer there.
Where am I going wrong with this JS code to find the average number from the user's input?
I want to keep entering numbers until -1 is entered. I think -1 is being counted as an input/
var count = 0;
var input;
var sum = 0;
while(input != -1){
input = parseInt(prompt("Enter a number"));
count++;
sum = sum + input;
sum = parseInt(sum);
average = parseFloat(sum/count);
}
alert("Average number is " + average);
This is the right order (without all the unnecessary parsing...)
var count = 0;
var input;
var sum = 0;
input = parseInt(prompt("Enter a number"));
while (input != -1) {
count++;
sum += input;
average = sum / count;
input = parseInt(prompt("Enter a number"));
}
alert("Average number is " + average);
DEMO
Note that you can calculate the average once outside of the loop and save some CPU.
You need to check after you take the input from the user.
while(input != -1){
input = parseInt(prompt("Enter a number"));
//The user enters -1 but still inside the while loop.
if(input != -1)
{
count++;
sum = sum + input;
}
sum = parseInt(sum);
average = parseFloat(sum/count);
}
Here is the function code you need.
var count = 0;
var input;
var sum = 0;
while(true){
input = parseInt(prompt("Enter a number"));
if(input != -1)
{
count++;
sum = sum + input;
sum = parseInt(sum);
}
else
break;
}
average = parseFloat(sum/count);
alert("Average number is " + average);
This will grab the average...Simply accumulate the values and then divide the total 'sum' by the number of 'inputs made'(in our case we just use the count++)
var count = 0;
var input;
var sum = 0;
while(input != -1){
count++;
input = prompt("Enter a number");
sum += input;
sum = parseInt(sum);
}
average = (sum/count);
alert("Average number is " + average);

If Statement? Don't add numbers into total variable if they are negative

Question, how do I make it so when an user-inputted number is negative, it isn't added to the total variable that will be ouput?
Code below!
function lab10logicInLoopsPart1()
{
lCounter = 1;
var total = 0;
userNumber = 0;
while(lCounter < 6) {
lCounter++;
userNumber = prompt("Enter a number.");
total += +userNumber;
document.write("Entered number was: " + userNumber + "\n");
}
document.write("\nTotal: " + total);
}
After prompting the user to enter a number, just check it's not a negative with an if statement:
if(userNumber >=0)
{ //do your stuff here}

Categories

Resources