尝试创建将我的年龄与其他人的年龄进行比较的函数

Kat*_*pil 1 javascript algorithm compare function

尝试创建一个函数来将我的年龄与其他人的年龄进行比较。显然,我错过了一些东西,因为无论输入输出是什么,总是相同的。请帮忙。

This is what I have so far:
function compareAge(name,age){
  let myAge=27;
  if (age = myAge){
    return `${name} is the same age as me.`
  }else if (age < myAge){
    return `${name} is younger than me.`
  }else if (age > myAge){
    return `${name} is older than me.`
  }
}
  console.log(compareAge('Kat',2));
Run Code Online (Sandbox Code Playgroud)

bru*_*550 5

在此 if 语句中,您将age赋值myAge并将结果传递给 if。

if (age = myAge){
Run Code Online (Sandbox Code Playgroud)

在 Javascript 中,任何整数的布尔值都是 true。

console.log(Boolean(27));
//prints true
Run Code Online (Sandbox Code Playgroud)

要实际比较这些值agemyAge您需要使用双等号运算符。像这样:

function compareAge(name,age){
  let myAge=27;
  if (age == myAge){
    return `${name} is the same age as me.`
  }else if (age < myAge){
    return `${name} is younger than me.`
  }else if (age > myAge){
    return `${name} is older than me.`
  }
}
  console.log(compareAge('Kat',2));
Run Code Online (Sandbox Code Playgroud)