'arr[' + index + ']': newValue “+”号报错了
时间: 2023-05-26 21:01:49 浏览: 43
这段代码中的问题在于 newValue 值两端的“ ”号。在数组中,元素的值通常是不带引号的。如果要存储字符串类型的值,应该直接写字符串而不是用引号括起来。正确的用法应该是:
arr[index] = newValue;
其中,newValue 是一个字符串,如果是数字类型的值,也不应该加引号。
相关问题
var res = ""; res += " 万位: " + arr[0] ; res += " 千位: " + arr[1] ; res += " 百位: " + arr[2] ; res += " 十位: " + arr[3] ; res += " 个位: " + arr[4] ; document.getElementById("result").innerHTML = res;实现输出结果换行
你可以在每个字符串后面添加 `<br>` 标签来实现输出结果换行,代码如下:
```
var res = "";
res += " 万位: " + arr[0] + "<br>";
res += " 千位: " + arr[1] + "<br>";
res += " 百位: " + arr[2] + "<br>";
res += " 十位: " + arr[3] + "<br>";
res += " 个位: " + arr[4] + "<br>";
document.getElementById("result").innerHTML = res;
```
这样就可以在网页上实现输出结果换行了。
有一个数组,内放10个整数,要求找出最小的数和它的下标。然后把它和数组中最前面的元素对换。+示例:输入:10+2+16+99+85+63+14+25+33+87+输出:2+1+2+10+16+99+8
以下是Python实现的代码和解释:
```python
# 定义数组
arr = [10, 2, 16, 99, 85, 63, 14, 25, 33, 87]
# 找出最小值和它的下标
min_value = arr[0] # 先假设第一个元素是最小值
min_index = 0 # 最小值的下标
for i in range(1, len(arr)):
if arr[i] < min_value:
min_value = arr[i]
min_index = i
# 将最小值和数组中最前面的元素对换
arr[0], arr[min_index] = arr[min_index], arr[0]
# 输出结果
print(min_value) # 输出:2
print(min_index) # 输出:1
print('+'.join(str(i) for i in arr)) # 输出:2+10+16+99+85+63+14+25+33+87
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)