Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Coding using "while" statement problem?

1. ## 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

• Sounds like homework! Why do you think you need two while statements?

Saracens, Northampton, Ulster, Leinster, Clermont and Toulon: I think these are the six teams that are going to push their way into the semi-finals ... Commentator, BBC News Channel

• Originally Posted by Philip M
Sounds like homework! Why do you think you need two while statements?

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

• That code is not Javascript - VBScript perhaps? And again, why do you think this simple calculation requires multiple while statements?

• Originally Posted by Philip M
That code is not Javascript - VBScript perhaps? And again, why do you think this simple calculation requires multiple while statements?
It could be VBSciprt but I thought it's just basic coding.

Is it possible to write the code using 1 while statement?

• Originally Posted by Alien_Coder
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
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.

Originally Posted by Alien_Coder
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
Ditto for this code except that the wrong result is well over 3⁷.

Originally Posted by Alien_Coder
Is it possible to write the code using 1 while statement?
That would be the most efficient way to write it. Doing it that way would require that you use the `Math.pow(base, exponent)` method.

I have a feeling that you're being asked to calculate each result without the use of built-in mathematical functions though. Doing so would require using two `while` statements as required by the problem statement.

• 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">
<meta charset="UTF-8" />

<title> Untitled </title>

<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;
}
</script>

</body>
</html>```

• Originally Posted by Alien_Coder
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?
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 DOM-based math.

Live Example: https://patrick.dark.name/web.dev/demos/math.2/

Demo Primary Code:
Code:
```<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet 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">
<title>Demo for CodingForums.com Thread 307182: Coding using "while" statement problem?</title>
<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, "annotation-xml");
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", "MathML-Content");
base += 1;
exponent = base;
}
JSRequiredNotice.parentNode.replaceChild(list, JSRequiredNotice);
}
})();
// ]]>
</script>
</section>
</body>
</html>```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•