I am stuck.
I have to prompt a number. and then to create a cycle of prompts related with the intiial prompt and then sum them. I am completely lost. how can I sum them?
let first_question=prompt("introduce the quantity of numbers you want to sum")
for (let i=0; i < first_question;i++)
{
let second_question= prompt ("introduce the number you want to sum");
}
do something like this:
var first_question = parseInt(prompt("Introduce the quantity of numbers you want to sum: "));
var sum = 0;
for (let i = 0; i < first_question; i++) {
let second_question = parseInt(prompt("Introduce the number you want to sum: "));
sum += second_question;
}
and at the end the variable sum will have the value you want.
Also note that prompt() will return a string, so I used parseInt() in order to transform it into a number
Related
let i = -1;
let total = 0;
let numPrompt;
do {
numPrompt = prompt("")
total += parseInt(numPrompt)
i++;
}
while (numPrompt != '0'); // do-while loop
let avg = total/i;
console.log(total)
console.log(avg)
I want user to input a number each time till they input zero which will output all their previous input, total and average.
I declare i = -1 to avoid counting the last input which is the zero.
in my code, I only manage to display the total of all my input but what I also want is all my previous inputs (prompt)printed out each row
You can use an array, which stores a list of things:
let i = 0;
let total = 0;
let numPrompt;
let numbers = [];
do {
numPrompt = prompt("")
total += parseInt(numPrompt)
if (numPrompt != '0') {
numbers.push(numPrompt);
i++;
}
} while (numPrompt != '0'); // do-while loop
let avg = total / i;
console.log(total)
console.log(avg)
console.log(numbers);
Setting i to -1 is a bit of a workaround -- the above code starts it at 0 and checks if the inputted number is 0 before incrementing it.
If you want to store all of your previous inputs, use an array.
let a=[], i, t;
while((i=+prompt())!==0) a.push(i);
console.log(`inputs: ${a}, total: ${t=a.reduce((a,c)=>a+c)}, avg: ${t/a.length}`);
var num = prompt("Enter a number");
for (var sum = 0; sum <= num; sum++) {
sum = sum + 1;
}
document.write(sum);
example when I enter 6 in the prompt it will sum 1+2+3+4+5+6 =21. but as of right now i can only print 123456 instead of 21.
Input received by you is a string and that's why it's contacting rather than adding to sum.
This is the best optimum solution as its time complexity is O(3) times only.
so, it's fast. rather than with brute force which is o(n);
var num = prompt("Enter a number");
function total(n) {
return n * (n + 1) / 2;
}
document.write(total(parseInt(num)));
The problem with your code is that you're using sum for the loop and for the answer. That's messing everything up. You can use a variable for the loop and another variable for the sum.
Maybe that works for you.
var num = prompt("Enter a number");
var sum = 0;
for(var i = 1; i <= num; i++) {
sum += i;
}
document.write(sum);
here is some change in your code.
note: (+) is used to convert string-number type to number type
var num = prompt("Enter a number");
const sum = Array.from(Array(+num + 1).keys()).reduce((prev, curr) => prev += curr, 0);
document.write(sum);
I am trying to solve a tiny program (average of notes) that request via prompt a number. I want that if the input it's not a number, the alert command show a number is needed.
If you check the code, prompt input uses Number() to convert the string to a number. But if I type some string the result is NaN and I tried to reasign the note variable in the while loop but something is wrong because the program continues executing the remaining code.
let subjects = Number(prompt('Type quantity of subjects: '));
let sum = 0;
while (isNaN(subjects)) {
alert('Type a number');
subjects = Number(prompt('Type quantity of subjects: '));
}
for (i = 1; i <= notes; i++) {
note = Number(prompt('Type note of subject' + i + ': '));
sum += note;
}
average = sum / subjects;
alert(average.toFixed(2));
I expect the program ask (via prompt) for a number everytime it's not.
In your code notes is undefined which is used in for loop. I think subjects should be there. And also convert note to Number. And use Unary Plus +. for converting string to number. Its faster.
let subjects = +prompt('Type quantity of subjects: ');
let sum = 0;
while (isNaN(subjects)) {
alert('Type a number');
subjects = +prompt('Type quantity of subjects: ');
}
for (let i = 1; i <= subjects; i++) {
note = +prompt('Type note of subject' + i + ': ');
sum += +note;
}
average = sum / subjects;
alert(average.toFixed(2));
I'm supposed to prompt the user to enter a string of numbers separated by spaces and alert the sum of those numbers. I'm trying to get the values into an array and then add them up, but it's not working. I've tried a ton of different ways. Help please!
var input = prompt("Enter a string of numbers separated by spaces");
var numbers = new Array (input.split(" "));
var sum = 0;
for(var i = 0; i < numbers.length; i++){
sum += numbers[i];
};
alert(sum);
JSFiddle: http://jsfiddle.net/mUqfX/2/
You're close, 2 issues with your code. First, .split returns an array so you don't need to wrap a new around it. Second, you need to parse the number otherwise your joining strings together. Try
var input = prompt("Enter a string of numbers separated by spaces");
var numbers = input.split(" ");
var sum = 0;
for(var i = 0; i < numbers.length; i++){
sum += parseInt(numbers[i]);
};
alert(sum);
You have 2 problems:
input.split(" ") returnss an array, so you don't need to place it in another array
Your numbers array contains strings, which you need to coerce to numbers to total them.
Try this:
var input = prompt("Enter a string of numbers separated by spaces");
var numbers = input.split(" ");
var sum = 0;
for(var i = 0; i < numbers.length; i++){
sum += parseInt(numbers[i]);
};
alert(sum);
I am NOT talking about adding elements together, but their values to another separate variable.
Like this:
var TOTAL = 0;
for (i=0; i<10; i++){
TOTAL += myArray[i]
}
With this code, TOTAL doesn't add mathematically element values together, but it adds them next to eachother, so if myArr[1] = 10 and myArr[2] = 10 then TOTAL will be 1010 instead of 20.
How should I write what I want ?
Thanks
Sounds like your array elements are Strings, try to convert them to Number when adding:
var total = 0;
for (var i=0; i<10; i++){
total += +myArray[i];
}
Note that I use the unary plus operator (+myArray[i]), this is one common way to make sure you are adding up numbers, not concatenating strings.
A quick way is to use the unary plus operator to make them numeric:
var TOTAL = 0;
for (var i = 0; i < 10; i++)
{
TOTAL += +myArray[i];
}
const myArray = [2, 4, 3];
const total = myArray.reduce(function(a,b){ return +a + +b; });
Make sure your array contains numbers and not string values. You can convert strings to numbers using parseInt(number, base)
var total = 0;
for(i=0; i<myArray.length; i++){
var number = parseInt(myArray[i], 10);
total += number;
}
Use parseInt or parseFloat (for floating point)
var total = 0;
for (i=0; i<10; i++)
total+=parseInt(myArray[i]);