gpt4 book ai didi

javascript - 替换字母javascript特定位置的下划线

转载 作者:行者123 更新时间:2023-11-28 08:14:26 24 4
gpt4 key购买 nike

我需要用字母替换一组位置的下划线。我已经创建了一个函数来在特定位置替换它,但我不确定我是否引用了它或者它是否有效。

function replaceAt(str,index,chr) {
if(index = -1) {
str = str;
} else {
str = str.substr(0,index) + chr + str.substr(index+1);
}
}
underscore = replaceAt(underscore, word.indexOf(letter * 2), letter);

这就是我此时正在使用的,但它说它是未定义的。有任何想法吗?完整代码如下。

在此先感谢,我只是新手,所以非常感谢任何帮助。

已编辑

代码现在看起来像下面的代码,片段已被编辑为当前代码。仍然没有打印出我需要的字母。

function replaceAt(str,index,chr) {
if(index != -1) {
str = str.substr(0,index) + chr + str.substr(index+1);
}
return str;
}
underscore = replaceAt(underscore, word.indexOf(letter * 2), letter);

/* Hangman Game CSS File */
body
{
background-color: #1B264F;
}

#main {
background-color: #5386E4;
height: 97vh;
width: 95vw;
vertical-align: middle;

}
canvas {
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
margin-top: auto;
margin-bottom: auto;
}

/* colour scheme hex

lightest blue: #5386E4;
middle blue: #274690;
darkest blue: #1B264F;
black color: #070707;
white color: #E2E2E3;

*/
<!DOCTYPE html>
<html>
<head>
<title>Hangman by Lachlan Teale</title>
<meta name="description" content="Lachlan Teale's Hangman Game 2015">
<meta name="keywords" content="hangman, lachlan, teale, Javascript">
<meta charset="UTF-8">
<meta name="author" content="Lachlan Teale">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<canvas id="main" width="1920" height="1080" style="border:1px solid #d3d3d3;"></canvas>

<script type="text/javascript">

//setting variables
var canvas = document.getElementById('main');
var context = canvas.getContext('2d');
var underscore = "";
var letter;
var complete = false;

//list of words
var wordList = ["apple", "peach", "pear", "mango", "banana", "paper", "pens", "laptop", "desk", "chair", "star", "sky", "moon", "sun", "planets"];

//picking a random word from the list
var word = wordList[Math.floor(Math.random() * wordList.length)];

//print out the underscores for the number of letters
for (i = 0; i < word.length; i++) {
var underscore = underscore + "_ ";
}

context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(underscore, canvas.width * 0.5, canvas.height * 0.70);

//correct letter text
context.font = 'bold 30pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.text
context.fillText("Correct Letters", canvas.width * 0.08, canvas.height * 0.05);

//Wrong Letter text
context.font = 'bold 30pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.text
context.fillText("Wrong Letters", canvas.width - (canvas.width * 0.08), canvas.height * 0.05);

//checking which button is pressed
window.addEventListener('keydown', function(event) {
if ( event.keyCode >= 65 && event.keyCode <= 90 ) {
context.clearRect(0, canvas.height * 0.80, canvas.width, canvas.height);
letter = String.fromCharCode( event.keyCode ).toLowerCase();
context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(letter, canvas.width * 0.5, canvas.height * 0.85);
}
});

//checking if back key is pressed
document.addEventListener('keydown', function(event) {
if ( event.keyCode == 8 ) {
context.clearRect(0, canvas.height * 0.80, canvas.width, canvas.height);
}
});

//function for replacing letter
function replaceAt(str,index,chr) {
if(index != -1) {
str = str.substr(0,index) + chr + str.substr(index+1);
}
return str;
}

//checking if enter is pressed
document.addEventListener('keydown', function(event) {
if( event.keyCode == 13 ) {

//checking if letter is in the word
alert(word);

alert(underscore + " " + letter + " " + word.indexOf(letter));

underscore = replaceAt(underscore, word.indexOf(letter * 2), letter);

context.clearRect(canvas.width * 0.4, canvas.height * 0.3, canvas.width *0.7, canvas.height * 70);
context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(underscore, canvas.width * 0.5, canvas.height * 0.70);


}
});


</script>
</body>
</html>

最佳答案

你的函数有几个问题。

  1. 您正在使用 = 而不是 ====== 进行赋值,而不是比较。

  2. 您永远不会从函数中返回任何东西。

所以纠正这两个:

function replaceAt(str,index,chr) {
if (index == -1) {
str = str;
} else {
str = str.substr(0,index) + chr + str.substr(index+1);
}
return str;
}

但是我们根本不需要第一部分,所以:

function replaceAt(str,index,chr) {
if (index != -1) {
str = str.substr(0,index) + chr + str.substr(index+1);
}
return str;
}

用法:

var str = "this is_a test";
var index = str.indexOf("_");
str = replaceAt(str, index, "|");
console.log(str); // "this is|a test"

值得注意的是,字符串也有一个replace 方法,它将替换第一次出现的子字符串:

var str = "this is_a test";
str = str.replace("_", "|");
console.log(str); // "this is|a test"

或者如果您使用带有 g 标志的正则表达式,则所有出现:

var str = "this is_a test_with two underscores";
str = str.replace(/_/g, "|");
console.log(str); // "this is|a test|with two underscores"

关于javascript - 替换字母javascript特定位置的下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com