0
Trying to solve skipping 13 in JavaScript
Many tall buildings, including hotels, skip the number 13 when numbering floors -- often going from floor 12 to floor 14. It is thought that the number 13 is unlucky. Write a program that will number 15 rooms starting from 1, skipping the number 13. Output to the console each room number in separate line.
16 odpowiedzi
+ 6
var countOfRooms = 15;
// Your code here
for (i=1;i<=16;i++){
if(i==13){
continue
}
console.log(i)
}
This worked for me ✌
+ 1
Yaroslav Vernigora thanks
+ 1
I think this is the correct way. Try this.
for (i=1; i<=countOfRooms+1; i++){
if(i==13) {
continue;
}
console.log(i);
}
0
Hi! For better help for you, Please, show us your code attempt! Thx!
0
var countOfRooms = 15;
// Your code here
for (i=1; i<=15; i++){
if (i==13){
comtinue;
}
document.write(i+"<br/>");
}
0
easy as heck, just print ALL OF THEM and skip 13th number! AND YOUR TEACHER WILL APPRECIATE IT!
joke aside. attempt it yourself.
edit: Simisola Osinowo oh bruh, you misspelled continue aand you didn't used countOfRooms variable in the for loop.
0
comtinue -> continue
0
Now it's saying document not defined
0
Instead of "document.write" -> insert "console.log"
0
Done that
var countOfRooms = 15;
// Your code here
for (i=1; i<=15; i++){
if (i==13){
continue;
}
console.log(i);
}
But now it does not print 16
0
so you have only 15 rooms. at the very beginning, the variable is assigned the value 15
0
Simisola Osinowo var I is 1 instead of 0
0
This solution works for me for any number of rooms (I tried to make it reusable)
var countOfRooms = 15;
// Your code here
if(countOfRooms >= 13){
for(var i = 1; i <= countOfRooms + 1; i++){
if(i == 13){
continue;
}
else{
console.log(i)
}
}
}
else{
for(var i = 1; i <= countOfRooms; i++){
console.log(i);
}
}
----------------------------------------------------------------------------------------------------------
Otherwise for this question specifically:
var countOfRooms = 15;
// Your code here
for(var i = 1; i <= countOfRooms + 1; i++){
if(i == 13){
continue;
}
else{
console.log(i);
}
}
edit: added reusable method and easier readability
0
Tijanihabeeb it needs to be 1 otherwise you'll end up printing a room number of '0'
If you want it to make the loop more reusable for any number of rooms, you'll need to increase the original var
For example:
for (n = 1; n <= countOfRooms; n++) {
if (n == 13) {
countOfRooms++
continue;
}
console.log(n);
}
0
var countOfRooms = 15;
// Your code here
for (i=1;i<=16;i++){
if(i==13){
continue
}
console.log(i)
}
//I hope, This will helful for the beginner .
0
var countOfRooms = 15;
// Your code here
for (let countOfRooms=1; countOfRooms<17; countOfRooms++) {
if(countOfRooms == 13) {
continue;
}
console.log(countOfRooms);
}
This worked for me.