Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 8 of 8

11022013, 01:24 PM #1
 Join Date
 Nov 2013
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Coding using "while" statement problem?
I'm not really sure where I should have posted this thread, please mod copy my thread to the correct section if it doesn't fit here.
I'm stuck in a problem, I want to write a simple code that calculates: 1^1,2^2,3^3,4^4,5^5 using two WHILE statements only, how can I achieve such a thing?
Example:
I have this code that calcultes 3^7:
N=3 Y=1
while N ≦ 7 do
Y=Y x 3
N = N + 1
print Y
please help guys
11022013, 01:30 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,593
 Thanks
 207
 Thanked 2,590 Times in 2,568 Posts
Sounds like homework! Why do you think you need two while statements?
Show your code, then someone will almost certainly help you correct/improve it. But we will not do your homework for you.
Saracens, Northampton, Ulster, Leinster, Clermont and Toulon: I think these are the six teams that are going to push their way into the semifinals ... Commentator, BBC News Channel
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
11022013, 02:07 PM
#3
 Join Date
 Nov 2013
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Yes it is a homework, I just have no idea how to do it.
There was a similar question which required me to calculate 3^7, here is my solution:
N=1 Y=3
while N ≦ 7 do
Y=Y x Y
N = N + 1
end while
print Y
I have no idea how to use multiple "while" statement in a code. any help would be appreciated
11022013, 03:20 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,593
 Thanks
 207
 Thanked 2,590 Times in 2,568 Posts
That code is not Javascript  VBScript perhaps? And again, why do you think this simple calculation requires multiple while statements?
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
11022013, 03:46 PM
#5
 Join Date
 Nov 2013
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
11032013, 12:52 AM
#6
 Join Date
 Mar 2006
 Location
 Splendora, Texas, United States of America
 Posts
 3,477
 Thanks
 36
 Thanked 301 Times in 295 Posts
I don't know why you're showing this code given that (A) it's not JavaScript and (B) it doesn't calculate a result of 3⁷ (2187). The result is 243 or 3 / 9 / 27 / 81 / 243 depending on how it's interpreted.
Ditto for this code except that the wrong result is well over 3⁷.
That would be the most efficient way to write it. Doing it that way would require that you use theMath.pow(base, exponent)
method.
I have a feeling that you're being asked to calculate each result without the use of builtin mathematical functions though. Doing so would require using twowhile
statements as required by the problem statement.
For every complex problem, there is an answer that is clear, simple, and wrong.
11042013, 04:45 AM
#7
Even if this is homework, you will not be able to present this to your instructor
unless you can explain the push() statement, but at least you will be able to
see the correct syntax to create a trial script for further testing.
Code:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8" /> <title> Untitled </title> </head> <body> <script type="text/javascript"> var N=1; var Y=1; var tarr = []; while (N <= 10) { var i=1; var y = N; var r = 1; Y = N; while (i <= y) { r *= Y; i++; } tarr.push(N+' ^ '+N + ' = ' + r); N = N + 1; } alert(tarr.join('\n')); </script> </body> </html>
11042013, 04:39 PM
#8
 Join Date
 Mar 2006
 Location
 Splendora, Texas, United States of America
 Posts
 3,477
 Thanks
 36
 Thanked 301 Times in 295 Posts
Well, since the cat's out of the bag, here's the "simple" (not really) code I wrote using JavaScript, XHTML, and MathML. I'm sure your instructor will be enthused to hear you relate how you mastered DOMbased math.
Live Example: https://patrick.dark.name/web.dev/demos/math.2/
Demo Primary Code:
Code:<?xml version="1.0" encoding="utf8"?> <?xmlstylesheet type="application/xml" href="../style.sheets/boilerplate.xslt"?> <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://www.w3.org/1998/Math/MathML"> <head> <title>Demo for CodingForums.com Thread 307182: Coding using "while" statement problem?</title> </head> <body> <h1 id="demo.title">Demo for CodingForums.com Thread 307182: <cite>Coding using "while" statement problem?</cite></h1> <section id="demo.description"> <h1>Demo Description</h1> <p>This demo demonstrates how to use JavaScript to calculate the results of <m:math><m:apply><m:power/><m:cn>1</m:cn><m:cn>¹</m:cn></m:apply></m:math>, <m:math><m:apply><m:power/><m:cn>2</m:cn><m:cn>²</m:cn></m:apply></m:math>, <m:math><m:apply><m:power/><m:cn>3</m:cn><m:cn>³</m:cn></m:apply></m:math>, <m:math><m:apply><m:power/><m:cn>4</m:cn><m:cn>⁴</m:cn></m:apply></m:math>, and <m:math><m:apply><m:power/><m:cn>5</m:cn><m:cn>⁵</m:cn></m:apply></m:math> using two <code>while</code> loops and without use of the <code>Math</code> object.</p> </section> <section id="demo"> <h1>Demo</h1> <p id="js.required.notice">This demo requires JavaScript.</p> <script> // <![CDATA[ // This script was validated at http://jshint.com/ using the following settings: /* jshint browser: true, curly: true, eqeqeq: true, devel: false, forin: true, immed: true, latedef: true, newcap: false, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */ (function () { "use strict"; function doExponentiation() { var XHTML = "http://www.w3.org/1999/xhtml"; var MathML = "http://www.w3.org/1998/Math/MathML"; var JSRequiredNotice = document.getElementById("js.required.notice"); var base = 1; var exponent = 1; var exponentCharacters = ["¹", "²", "³", "⁴", "⁵"]; var maxBaseAndExponent = 5; var product = null; var list = document.createElementNS(XHTML, "ul"); var listItem = null; var math = null; var mathSemantics = null; var presentationalMathRow = null; var presentationalMathBaseAndSuperscript = null; var presentationalMathBase = null; var presentationalMathExponent = null; var presentationalMathEqualityOperator = null; var presentationalMathProduct = null; var mathAnnotationXML = null; var contentMathEquationApplier = null; var contentMathEquationApplication = null; var contentMathExponentApplier = null; var contentMathExponentApplication = null; var contentMathBase = null; var contentMathExponent = null; var contentMathProduct = null; while (base <= maxBaseAndExponent) { product = base; while (exponent > 1) { product *= base; exponent = 1; } listItem = document.createElementNS(XHTML, "li"); math = document.createElementNS(MathML, "math"); mathSemantics = document.createElementNS(MathML, "semantics"); presentationalMathRow = document.createElementNS(MathML, "mrow"); presentationalMathBaseAndSuperscript = document.createElementNS(MathML, "msup"); presentationalMathBase = document.createElementNS(MathML, "mn"); presentationalMathExponent = document.createElementNS(MathML, "mn"); presentationalMathEqualityOperator = document.createElementNS(MathML, "mo"); presentationalMathProduct = document.createElementNS(MathML, "mn"); presentationalMathBase.textContent = base; presentationalMathExponent.textContent = exponentCharacters[base  1]; presentationalMathEqualityOperator.textContent = "="; presentationalMathProduct.textContent = product; mathAnnotationXML = document.createElementNS(MathML, "annotationxml"); contentMathEquationApplier = document.createElementNS(MathML, "apply"); contentMathEquationApplication = document.createElementNS(MathML, "eq"); contentMathExponentApplier = document.createElementNS(MathML, "apply"); contentMathExponentApplication = document.createElementNS(MathML, "pow"); contentMathBase = document.createElementNS(MathML, "cn"); contentMathExponent = document.createElementNS(MathML, "cn"); contentMathProduct = document.createElementNS(MathML, "cn"); contentMathBase.textContent = base; contentMathExponent.textContent = exponentCharacters[base  1]; contentMathProduct.textContent = product; presentationalMathBaseAndSuperscript.appendChild(presentationalMathBase); presentationalMathBaseAndSuperscript.appendChild(presentationalMathExponent); presentationalMathRow.appendChild(presentationalMathBaseAndSuperscript); presentationalMathRow.appendChild(presentationalMathEqualityOperator); presentationalMathRow.appendChild(presentationalMathProduct); contentMathExponentApplication.appendChild(contentMathBase); contentMathExponentApplication.appendChild(contentMathExponent); contentMathExponentApplier.appendChild(contentMathExponentApplication); contentMathEquationApplication.appendChild(contentMathExponentApplier); contentMathEquationApplication.appendChild(contentMathProduct); contentMathEquationApplier.appendChild(contentMathEquationApplication); mathAnnotationXML.appendChild(contentMathEquationApplier); mathSemantics.appendChild(presentationalMathRow); mathSemantics.appendChild(mathAnnotationXML); math.appendChild(mathSemantics); listItem.appendChild(math); list.appendChild(listItem); presentationalMathEqualityOperator.setAttribute("class", "equality"); mathAnnotationXML.setAttribute("encoding", "MathMLContent"); base += 1; exponent = base; } JSRequiredNotice.parentNode.replaceChild(list, JSRequiredNotice); } document.addEventListener("DOMContentLoaded", doExponentiation); })(); // ]]> </script> </section> </body> </html>
For every complex problem, there is an answer that is clear, simple, and wrong.