The function MoveDir doesnt work correctly, its supposed to use the search strin
ID: 3771108 • Letter: T
Question
The function MoveDir doesnt work correctly, its supposed to use the search string to define the number where the word starts that is typed into the box in the input at the bottom. then call the function in the in statements. full code here:
https://ideone.com/NK0umn
unfortunately it isnt working to call the functions based off the string number.
function MoveDir() {
var str = "updownleftright";
dirword = "up";
dirword = document.getElementById('dirBox');
var wordPos = str.search(dirword);
if (wordPos === 0) {
MoveUp();
} else if (wordPos === 2) {
MoveDown();
} else if (wordPos === 6) {
MoveLeft();
} else if (wordPos === 10) {
MoveRight();
} else {
Alert("That is not a valid direction, try again");
}
}
</script>
</head>
<body>
<div>
<p>
<img id="dotImg" alt="Dot Image" src="x1y1.jpg">
</p>
<input type="button" value="Move Up">
<input type="button" value="Move Down">
<input type="button" value="Move Right">
<input type="button" value="Move Left">
<br>
<input type="text" id="dirBox" size=12 value="">
<input type="button" value="Take A Step">
</div>
Explanation / Answer
I have found you problem.
Problem is in selection of functon to search string and Assignment operator in If-else block of MoveDire() function.
instead of Search() you have to use indexOf() function which search the substring in given string and gives you index.
Problem with Search() is that It expects Regular expresion as parameters or if you pass normal string literal It will first convert in to internal Regex and then matches in given String. On the basis of Match it will return value.
But indexOf() will expects normal String literal only So it will search string literal no regex conversion takes place. So if you use indexOf you problem will half solve.
Next half problem will be solve if you use correct comparision operator. Comparision operator is "==" not "=". Look below you have used invalid operator it assign the value. Will not compare and other mistake is "Alert" in which 'A" is capital. Correct version is "alert()".
Please find correct code below.
<html>
<head>
<title> Die Rolls </title>
<script type="text/javascript">
y = 1
x = 1
function MoveUp() {
y = y + 1;
if (y > 4) {
y = 4;
}
Imagedot = 'x' + x + 'y' + y + '.jpg';
document.getElementById('dotImg').src = Imagedot;
}
function MoveDown() {
y = y - 1;
if (y < 1) {
y = 1;
}
Imagedot = 'x' + x + 'y' + y + '.jpg';
document.getElementById('dotImg').src = Imagedot;
}
function MoveRight() {
x = x + 1;
if (x > 4) {
x = 4;
}
Imagedot = 'x' + x + 'y' + y + '.jpg';
document.getElementById('dotImg').src = Imagedot;
}
function MoveLeft() {
x = x - 1;
if (x < 1) {
x = 1;
}
Imagedot = 'x' + x + 'y' + y + '.jpg';
document.getElementById('dotImg').src = Imagedot;
}
function MoveDir() {
var str = "updownleftright";
var dirword = document.getElementById("dirBox");
var wordPos = str.indexOf(dirword.value); // Please look Here
if (wordPos ==0) {
MoveUp();
} else if (wordPos ==2) {
MoveDown();
} else if (wordPos ==6) {
MoveLeft();
} else if (wordPos ==10) {
MoveRight();
} else {
alert("That is not a valid direction, try again");
}
}
</script>
</head>
<body>
<div>
<p>
<img id="dotImg" alt="Dot Image" src="x1y1.jpg">
</p>
<input type="button" value="Move Up">
<input type="button" value="Move Down">
<input type="button" value="Move Right">
<input type="button" value="Move Left">
<br>
<input type="text" id="dirBox" size=12 value="">
<input type="button" value="Take A Step">
</div>
</body>
</html>
if (wordPos = 0) {
MoveUp();
} else if (wordPos = 2) {
MoveDown();
} else if (wordPos = 6) {
MoveLeft();
} else if (wordPos = 10) {
MoveRight();
} else {
Alert("That is not a valid direction, try again");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.