python
name = '123'
age = 23
print(f '姓名: {name}')
JS
<script>
window.alert(`你好,${name}!`)
document.write(`<h1>你好,${name}!</h1>`)
typeof
#判断类型
#JavaScript中还可以使用const关键字定义常量
//JavaScript中定义变量可以使用var或let关键字
//变量的命名跟python的变量命名规则相同,除了可以使用$
//变量可以大致分为基本数据类型和对象类型(object)两大类
//基本数据类型包括:number boolean string null undefined symbol
//用typeof 判断数据类型
//JavaScript中还可以使用const关键字定义常量
//算术运算: +-*/ % **
//比较运算: == != > < >= <= === !==
//逻辑运算: && (and) ||(or) !(not)
let stu = {name:'zeng', age:23}
stu.name
"zeng"
stu.age
23
let name = prompt('请输入你的名字:')
if (!!name && name.trim() != '') {
alert(`你好, ${name}`)
} else {
alert('大家好!')
}
let year = parseInt(prompt('请输入年份:'))
if (year%4 == 0 && year%100 != 0 || year % 400 == 0) {
alert(`${year}是闰年`)
} else {
alert('不是闰年')
}
let year = parseInt(prompt('请输入年份:'))
let isLeap = year%4 == 0 && year%100 != 0
|| year % 400 == 0
alert(`${year}年${isLeap?'是':'不是'}闰年`)
#for循环
let total = 0
for (let i = 1; i <= 100; i += 1) {
total += i
}
document.write(total)
#while循环
let total = 0
i = 1
while (i <= 100) {
total += i
i += 1
}
document.write(total)
#do while循环
let total = 0
i = 1
do {
total += i
i += 1
} while (i <= 100)
document.write(total)
</script>
// 变量数组元素
let nums = [1, 2, 3, 4, 5]
// for (let idx = 0; idx < nums.length; idx += 1) {
// alert(nums[idx])
// }
for (num of nums) {
alert(num)
}
// 遍历对象属性
let stu = {
name: 'Z',
age:44,
sex:'男',
friends:['h', 'y'],
car: {
'brand': 'QQ',
'maxSpeed':'55'
}
}
for (prop in stu) {
alert(stu[prop])
}
乘法表
for ( let i = 1; i <= 9; i += 1) {
document.write('<p>')
for (let j = 1; j <= i; j += 1) {
document.write(`${i}×${j}=${i * j} `)
}
document.write('</p>')
}