javascript - 我是 JavaScript 新手,对如何观察、测试我的代码以查看它是否“有效”感到困惑
问题描述
我正在尝试自学 JavaScript - 大部分时间每天都花几个小时学习 JavaScript,持续了大约两个月。我主要使用 Codecademy,但也使用 YouTube、关于不同概念的博客文章、MDN 和其他文档站点、learnjavascript.com 等进行补充。
我发现我需要学习的不仅仅是 JavaScript 这种语言,因为我相信大多数参加过这段旅程的人都会发现。我已经到了在代码中遇到小错误的地步,我不确定如何观察、测试和修复它。
例如,作为 Codecademy 项目的一部分,我的任务是使用 vanilla JS 重新创建旧 JS 库的方法:
const _ = {
clamp(number,lower,upper) {
const lowNum = Math.max(number,lower);
const clampNum = Math.min(lowNum,upper);
return clampNum;
},
inRange(num,start,end) {
if (end === undefined) {
end === start && start === 0;
}
if (start > end) {
let temp = end;
end = start;
start = temp;
}
const isInRange = start <= num && num < end ? true : false;
return isInRange;
}
};
我知道打印出我期望代码执行的操作是个好主意,但坦率地说,我不确定如何编写它,在哪里插入它,以及如果/当该代码抛出错误时该怎么办. 我已经尝试console.log
为每个步骤插入表达式,但它们只是(始终)抛出了在此之前不存在/不明显的语法错误。因此,我怀疑我错误地使用了该代码,如下所示,在我评论所有内容之前,一直抛出语法错误
const _ = {
clamp(number,lower,upper) {
const lowNum = Math.max(number,lower);
const clampNum = Math.min(lowNum,upper);
return clampNum;
},
//add method called inRange, add 3 params: number, start, end
inRange(number,start,end) {
//create if state to check to see if end is undefined
if (end === undefined) {
//set end equal to start, set start equal to 0
end === start && start === 0;
}
// console.log('this is end ' + end + ' and this is start ' + start);
//add another if statement to check whether start is bigger than end by swapping the start and end values
if (start > end) {
//create var called temp set to current end value (do they really actually mean to set it to the end value???)
let temp = end;
// console.log('this is end ' + end + ' and this is temp ' + temp)
//set end equal to start
end = start;
// console.log('this is end ' + end + ' and this is temp ' + temp)
//finally set start equal to temp
start = temp;
}
// console.log('this is end ' + end + ' and this is temp ' + temp)
//create var called isInRange, set it equal to a boolean expression that checks if start is smaller than or equal to number and if number is smaller than end
const isInRange = {
if (start <= number && number < end) {
return true
}
//finally return value of isInRange (again, return VALUE or just return the var???)
return isInRange;
}
};
// console.log(inRange(3,2,4))
诚然,我不是一个逻辑思考者,而且我发现这些练习非常困难。我曾多次(一天)灰心丧气,但我正在努力寻找继续前进的方法,因为(对我而言)学习这一点很重要。
我希望得到一些关于如何最好地观察、测试的指示(这些是 Codecademy 上使用的流行术语,与如何弄清楚为什么代码没有按预期工作有关,所以我希望我能在没有听起来的情况下表达我的观点太天真)我的代码,这样我就可以避免让它变得更糟,同时实际上也揭示了我需要关注和修复的内容。也许这是一个白日梦而不是可能的事情?我听从这里的专家分享他们对此的任何见解。谢谢你。
解决方案
推荐阅读
- apache-flink - Apache Flink 开发文档?
- php - 用于在 Twig 中匹配数字的正则表达式
- r - read.csv.fdff 错误:无法分配大小为 6607642.0 Gb 的向量
- python - 如何从我的 kivy 程序中删除“sdl2 - ImportError”和“pil - ModuleNotFoundError”
- javascript - 到处都使用同一个类的 Javascript 验证
- java - 有没有办法在java中模拟属性选择器?
- spring-boot - 如何使用身份验证服务器(OAuth2,0 和 JWT)、Zuul 和 Eureka 服务实现微服务?
- c# - 博士 = cmd.ExecuteReader(); dr在哪里返回空值如何解决?
- python - 我对 keras conv1D 层的输入应该是什么以及 input_shape 应该是什么?
- ios - 应用因 Info.plist 文件麦克风中缺少目的字符串而被拒绝,但我无法在 info.plist 中添加描述