Question

I need help with a javascript assignment: Write a program called sum_number.js, that calculates the sum...

I need help with a javascript assignment:

Write a program called sum_number.js, that calculates the sum of the numbers contained in a string and displays the result. Use a string containing “2155” as your input value. For example, given the input , let num = “22”, I would sum 2 + 2 for a result of 4. The power is the amps at a given hour times the voltage at the same hour. Calculate the average power consumed for the day and display it at the end. i.e

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<!DOCTYPE html>
<html>
<body>

Enter Number: <input type="text" id="num" name="txt"
onchange="myFunction(this.value)">

<script>
function myFunction(val) {
sum=0,n=0;
num=parseInt(val);
while(num > 0)
{
n = num % 10;
sum = sum + n;
num = num / 10;
}
alert(Math.floor(sum));
}
</script>

</body>
</html>

Add a comment
Know the answer?
Add Answer to:
I need help with a javascript assignment: Write a program called sum_number.js, that calculates the sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program using a function called sum that calculates the following summation: p=2+5 F...

    Write a C++ program using a function called sum that calculates the following summation: p=2+5 F = i2 + 5 i=1 Where n is provided by the user in the main function. The function calculates the sum and return it to the main function to display it.

  • I need help with this C++ assignment. Create a program which will implement the sum of...

    I need help with this C++ assignment. Create a program which will implement the sum of the numbers between 1 and n where n is an integer. When completed, the program will allow the user to input a positive integer (n) then the program will add all numbers between 1 and n (including n) and output the sum. For example, if the user enters 8 then the program will sum the numbers 1 through 8 and will output the sum....

  • Create a function in JavaScript called "sum" that takes in an array as a parameter and...

    Create a function in JavaScript called "sum" that takes in an array as a parameter and returns the sum of all of the EVEN numbers in a given array. Please note: the array passed in can have any data type, i.e. Strings, Numbers, Booleans, undefined, etc. HINT: the expression typeof will help discern var types. For example if I have: var x = 4, and I do typeof(x) - the output would be "Number" Example run: sum([1, 4, 8, 10,...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • JavaScript Just use javascript, dont include HTML Follow the instructions bellow for a good rate Create...

    JavaScript Just use javascript, dont include HTML Follow the instructions bellow for a good rate Create a program that will display the result of three functions: a) sum the elements from the Fibonacci sequence b) divide them by randomly generated number c) calculate factorial from the rounded result of previous operation Functions should be stored in one, separate file. The main program should run from the app.js that is provided bellow, the other file should be called module.js that includes...

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • JavaScript expert, I need your help : My program doesn't work. I have the following program,...

    JavaScript expert, I need your help : My program doesn't work. I have the following program, it is simple web page: Create an object called person with name = John, age = 50. Then, access the object to display "John is 50 years old”. I have written some code here. Hint: Create an object with the var keyword, followed by a name and an "=" sign. Put the properties and values inside the {}; signs Note: Use getElementById() and innerHTML...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT