无法在 js 中打印 foobar 只能打印 foo 和 bar
Not able to print foobar in js only prints foo and bar
- 我正在学习 js
- 你能告诉我以下任务的代码是否正确吗......
- 我能够打印foo和酒吧
- 但无法打印福巴
http://jsfiddle.net/1u1o2es7/
// Looping from 1 to 100 print out the following
// If the number is divisible by 3, log X foo
// if the number is divisible by 5, log X bar
// If the number is divisible by 15, log X foobar
// Only one output per number
// Expected output:
//
// 1
// 2
// 3 foo
// 4
// 5 bar
// 6 foo
// ...
// 15 foobar
// ...
// 100 bar
for(i=1; i<=100; i++){
console.log(i);
//var str = "";
if(i%3 == 0) {
//str = "foo";
console.log("foo");
}
else if(i%5 == 0) {
console.log("bar");
}
else if(i%3 == 0 && i%5 == 0) {
console.log("foobar");
}
}
你在 15 岁时只得到"foo"的原因是if (15%3 == 0)
计算结果为 true,并且您不会进入任何其他情况。
如果大小写,请将else if(i%3 == 0 && i%5 == 0)
移到顶部。
for(i=1; i<=100; i++){
console.log(i);
if(i%3 == 0 && i%5 == 0) {
console.log("foobar");
}
else if(i%5 == 0) {
console.log("bar");
}
else if(i%3 == 0) {
console.log("foo");
}
}
这就是你想要的。
您可以使用浏览器开发人员工具逐步完成 JavaScript 的编译器。只需按 f12 并转到脚本部分,您就可以设置一个断点并查看 javascript 引擎在做什么。
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- JavaScript打印功能使日历停止工作
- esri javascript异步打印
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- jQuery/JavaScript在线公文包表单-打印样式表
- 如何打印嵌套对象的所有值
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 打印预览没有应用程序页眉和页脚的html表格
- 正在打印图表上的文本
- Rhino打印功能
- if(foo!==null)的计算结果为true,即使foo为null
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- 如何用javascript打印php数组
- Javascript,从静态函数中打印全局对象
- 试图打印/调用/或提醒数组中的对象:Getting Undefined
- 按压“;输入“;将值打印到控制台日志
- 将数组的拼接分配给自身时,Javascript控制台打印不起作用
- 无法在 js 中打印 foobar 只能打印 foo 和 bar
- 如何使用名称为foo.txt的json对象.我想打印这个对象的元素
- 为什么 foo: { bar: 10} 在 Chrome 控制台中执行时打印 10 |Javascript