Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Coding using "while" statement problem?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-02-2013, 02:24 PM PM User | #1 Alien_Coder New to the CF scene   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
 11-02-2013, 02:30 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,489 Thanks: 200 Thanked 2,471 Times in 2,449 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 semi-finals ... 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.
11-02-2013, 03:07 PM   PM User | #3
Alien_Coder
New to the CF scene

Join Date: Nov 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Philip M 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.
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

 11-02-2013, 04:20 PM PM User | #4 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,489 Thanks: 200 Thanked 2,471 Times in 2,449 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.
11-02-2013, 04:46 PM   PM User | #5
Alien_Coder
New to the CF scene

Join Date: Nov 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 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?

11-03-2013, 01:52 AM   PM User | #6
Arbitrator
Senior Coder

Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 3,139
Thanks: 11
Thanked 242 Times in 238 Posts
Quote:
 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.

Quote:
 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⁷.

Quote:
 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.
__________________
For every complex problem, there is an answer that is clear, simple, and wrong.

 11-04-2013, 05:45 AM PM User | #7 jmrker Senior Coder     Join Date: Aug 2006 Location: FL Posts: 2,936 Thanks: 36 Thanked 478 Times in 472 Posts 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: ``` Untitled ```
11-04-2013, 05:39 PM   PM User | #8
Arbitrator
Senior Coder

Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 3,139
Thanks: 11
Thanked 242 Times in 238 Posts
Quote:
 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>```
__________________
For every complex problem, there is an answer that is clear, simple, and wrong.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:36 PM.