2

This is for some homework of mine. Javascript has thus so far been the most difficult for me to grasp. Here's a copy of my assignment:

Copy the following code into new file.

<html>>
<body>
<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;
document.write(namAr + "<br /> Length:" + namArLen); 
</script> </body>
</html>

When you execute this code you will see that after you entered your name it was loaded into namAr array. Your output will look like this:

C,o,l,o,r,a,d,o, ,M,a,r,k, ,U,d,a,l,l 
Length: 19

Your task: Add a small iteration that will display your name in reverse order. For example if senator’s state and name as I entered as:

Colorado Mark Udall

your output will be

C,o,l,o,r,a,d,o, ,M,a,r,k, ,U,d,a,l,l 
Length: 19
lladU kraM odaroloC

I'm trying to achieve the bold part. They check our coding. I'm not looking for an answer as much as I am a sort of walkthrough or explaination. Anything help, thanks!

RESOLVED Figured it out, thanks! All great answers- you guys are so smart!

0

2 Answers 2

2
<html>
<body>
<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;


document.write(namAr + "<br /> Length:" + namArLen);



var i;
for (i = namArLen-1; i >=0;i--)
{
var result = document.write(namAr[i]);
}
</script> 


</body> </html>

you can use loop

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of

var namAr = namIn.split("");

try

var namAr = namIn.split("").reverse();

1 Comment

As it is homework, your teacher probably wants something that uses techniques of programming, like the loop answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.